我有文本输入项并使用 select() 我可以突出显示其中的文本,但是有时文本已经出于正当原因被选择,当我运行 select() 时,它会取消选择它,我认为,是否可以检查输入项是否被选中?
最佳答案
您可以使用 http://www.codetoad.com/javascript_get_selected_text.asp 检查选择.
您必须检查所选内容是否在您的输入项中。
var selecttxt = '';
if (window.getSelection) {
selecttxt = window.getSelection();
} else if (document.getSelection) {
selecttxt = document.getSelection();
} else if (document.selection) {
selecttxt = document.selection.createRange().text;
}
var textofinput = [...]; // put code to find text in input here
if (textofinput.indexOf(selecttxt) !== -1) {
// part of the text in the input is selected
alert('Conditions met!');
}
关于javascript - 检查输入项文本是否已被选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3046623/