Selection.containsNode()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

Selection.containsNode() 判断指定的节点是否包含在 Selection 中 (是否被选中).

语法

sel.containsNode(aNode,aPartlyContained)

参数

aNode

用于判断是否包含在 Selection 中的那个节点

aPartlyContained

当此参数为true 时, 当selection 包含节点 aNode 的一部分或全部时,containsNode() 返回 true. 当此参数为false 时, 只有当 selection 完全包含节点 aNode 时,containsNode() 才返回 true.

例子

 /* 检查 body 中是否有节点被选中 */
 console.log(window.getSelection().containsNode(document.body, true));

规范

Specification
Selection API
# dom-selection-containsnode

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
containsNode
partialContainment parameter is optional

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

参考