Window.getSelection extension
- Returns a Selection object representing the text range selected by the user or the current position of the cursor
- Example:
window.getSelection().toString()
- Extension method:
1、getRangeAt
返回选区开始的节点(Node)
因为通常情况下用户只能选择一个范围,所以只有一个选区(range),此方法一般为getRangeAt(0)
2、toString
返回当前选区的纯文本内容。
3、containsNode
判断某一个node是否为当前选区的一部分。
4、deleteFromDocument
从页面中删除选区中的内容
4. Get the selected parent node
// startContainer启动容器
window.getSelection().getRangeAt(0).startContainer.parentNode
console.log(window.getSelection().getRangeAt(0)) console.log(window.getSelection().getRangeAt(0).startContainer)
console.log(window.getSelection().getRangeAt(0).startContainer.parentNode)