您的位置:首页 > Web前端 > JavaScript

JS获取html页面上选中内容

2016-05-10 22:49 681 查看
新的html浏览器都支持获取页面中选中文本的操作了。

看看js中提供的操作:

在chrome内:

>document.getSelection();
Selection {anchorNode: text, anchorOffset: 4, focusNode: text, focusOffset: 4, 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();
就可以输出获取到的文本了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: