这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
Selection.containsNode()
method indicates whether a specfied node is part of the selection.
sel.containsNode(node, partialContainment)
node
The node that is being looked for in the selection.
partialContainment
可选
true
,
containsNode()
返回
true
when a part of the node is part of the selection. When
false
,
containsNode()
only returns
true
when the entire node is part of the selection. If not specified, the default value
false
被使用。
This snippet checks whether anything inside the body element is selected.
console.log(window.getSelection().containsNode(document.body, true));
In this example, a message appears when you select the secret word. It uses
addEventListener()
to check for
selectionchange
事件。
<p>Can you find the secret word?</p> <p>Hmm, where <span id="secret" style="color:transparent">SECRET</span> could it be?</p> <p id="win" hidden>You found it!</p>
const secret = document.getElementById('secret');
const win = document.getElementById('win');
// Listen for selection changes
document.addEventListener('selectionchange', () => {
const selection = window.getSelection();
const found = selection.containsNode(secret);
win.toggleAttribute('hidden', !found);
});
| 规范 | 状态 | 注释 |
|---|---|---|
|
选定 API
The definition of 'Selection.containsNode()' in that specification. |
工作草案 | Current |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
containsNode
|
Chrome Yes | Edge 12 |
Firefox
4
|
IE No | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes |
Firefox Android
4
|
Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
partialContainment
parameter is optional
|
Chrome Yes | Edge ≤79 | Firefox 55 | IE No | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 55 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
不支持
实验。期望将来行为有所改变。
见实现注意事项。
Selection
, the interface it belongs to.
Selection
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument