var txt = '';
if (window.getSelection) {
txt = window.getSelection();
}
else if (document.getSelection) {
txt = document.getSelection();
}
else if (document.selection) {
txt = document.selection.createRange().text;
}
alert(txt);这段代码可以适用在其他标签里的文本的选中文本,但是当我选中的是input里的标签,确无补于事(FF的问题),始终是为空值。
请问高手谁知道如何获得在input标签里选中的文本。在各大主流浏览器上都可以使用
if (window.getSelection) {
txt = window.getSelection();
}
else if (document.getSelection) {
txt = document.getSelection();
}
else if (document.selection) {
txt = document.selection.createRange().text;
}
alert(txt);这段代码可以适用在其他标签里的文本的选中文本,但是当我选中的是input里的标签,确无补于事(FF的问题),始终是为空值。
请问高手谁知道如何获得在input标签里选中的文本。在各大主流浏览器上都可以使用
<button onclick="alert(getInputSelText('theforever'));" >显示选中内容</button>
<script type="text/javascript">
function getInputSelText(id) {
var obj = document.getElementById(id);
var selectedText = "";
if (window.getSelection) {
selectedText = window.getSelection().toString();
}
if (selectedText == "" && document.getSelection) {
selectedText = document.getSelection();
}
if (selectedText == "" && document.selection) {
selectedText = document.selection.createRange().text;
}
if (selectedText == "" && typeof(obj) != 'undefined' && obj != null && obj.selectionStart != undefined && obj.selectionEnd != undefined) {
selectedText = obj.value.substring(obj.selectionStart, obj.selectionEnd);
}
return selectedText;
}
</script>