jquery获取文本框中被选中的内容
2个回答
展开全部
<script>
function GetSelectedText() {
var selText = "";
if (window.getSelection) { // all browsers, except IE before version 9
if (document.activeElement &&
(document.activeElement.tagName.toLowerCase() == "textarea" ||
document.activeElement.tagName.toLowerCase() == "input")) {
var text = document.activeElement.value;
selText = text.substring(document.activeElement.selectionStart,
document.activeElement.selectionEnd);
} else {
var selRange = window.getSelection();
selText = selRange.toString();
}
} else {
if (document.selection.createRange) { // Internet Explorer
var range = document.selection.createRange();
selText = range.text;
}
}
return selText;
}
function OnSelectInput(input) {
selText = GetSelectedText();
alert("The contents of the current selection are\n" + selText);
}
</script>
<input size="40" value="Select a part of this text!" onselect="OnSelectInput (this)">
来源: help.dottoro.com/ljmwkbaj.php
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询