天天看點

JS擷取html頁面上選中内容

新的html浏覽器都支援擷取頁面中選中文本的操作了。

看看js中提供的操作:

在chrome内:

>document.getSelection();
Selection {anchorNode: text, anchorOffset: , focusNode: text, focusOffset: , isCollapsed: true…}
>>anchorNode:text
>>anchorOffset:0
>>baseNode:text
>>baseOffset:0
>>extentNode:text
>>extentOffset:9
>>focusNode:text
>>focusOffset:9
>>isCollapsed:false
>>rangeCount:1
>>type:"Range"
>>__proto__:Selection
           

在Microsoft的Edge浏覽器内,也可以使用

document.getSelection()

,而舊的IE10可以使用

document.selection

使用

document.getSelection().toString();

就可以輸出擷取到的文本了。

繼續閱讀