2011-03-19 86 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); 
};