2
我正在嘗試從用戶中獲取所選文本(用戶突出顯示的突出顯示的文本)。如何獲取突出顯示的文本?
我有以下幾點:
function getSelectedTexts(){
var t = '';
if(window.getSelection){
t = window.getSelection();
console.log('1');
}else if(document.getSelection){
t = document.getSelection();
console.log('2');
}else if(document.selection){
console.log('3');
t = document.selection.createRange().text;
}
return t;
}
$('.text_speech').live('click',function(e){
e.preventDefault();
var textTest='';
textTest=getSelectedTexts();
console.log(textTest);
})
我的控制檯返回
1
>Selection <------object
anchorNode: Text
anchorOffset: 2
baseNode: Text
baseOffset: 2
extentNode: Text
extentOffset: 1
focusNode: Text
focusOffset: 1
isCollapsed: false
rangeCount: 1
type: "Range"
__proto__: Selection
我不知道怎麼去選擇的文本。任何人都可以幫助我呢?非常感謝!
Ref。選擇(MDN)https://developer.mozilla.org/en-US/docs/DOM/Selection – prashanth