Possible Duplicate:
Check checkbox checked property using jQuery
谁能告诉我如何使用 jquery 检查 html 文本框中的值是否被选中。例如,如果选择文本框中的值,则文本将被选择为蓝色背景。
最佳答案
看这个:http://jsfiddle.net/rT5vR/
var t = '';
if(window.getSelection) {
t = window.getSelection();
} else if(document.getSelection) {
t = document.getSelection();
} else if(document.selection) {
t = document.selection.createRange().text;
}
return t;
}
$("#myElement").select(function(eventObject) {
alert(getSelected().toString());
});
或者
$('#myElement').select(function(e) {
var start = e.target.selectionStart;
var end = e.target.selectionEnd;
alert($('#myElement').val().substring(start, end));
});
第二个就完美了。不知道第一个跨浏览器有多少......
关于jquery - 检查使用 jquery 选择文本框中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13949302/