2011-03-19 57 views

回答

2

我已经编写了一个跨浏览器功能,用于获取在textarea或文本输入中选择的文本,并且在一些最终版本之后,想想我见过的最好的一个。我之前已经将它发布在Stack Overflow上。这里有一个例子:Is there an Internet Explorer approved substitute for selectionStart and selectionEnd?

要当用户在一个文本选择检测,你可以使用select事件:中

var textarea = document.getElementById("some_id"); 
textarea.onselect = function() { 
    var selection = getInputSelection(textarea); 
    var selectedText = textarea.value.slice(selection.start, selection.end); 
    console.log("Selected text: " + selectedText); 
}; 
相关问题