Node.compareDocumentPosition()
method reports the position of the given node relative to another node in any document — not just the given node’s document.
返回值为 bitmask of the following values:
| 名称 | 值 |
|---|---|
DOCUMENT_POSITION_DISCONNECTED
|
1 |
DOCUMENT_POSITION_PRECEDING
|
2 |
DOCUMENT_POSITION_FOLLOWING
|
4 |
DOCUMENT_POSITION_CONTAINS
|
8 |
DOCUMENT_POSITION_CONTAINED_BY
|
16 |
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
|
32 |
compareMask = node.compareDocumentPosition(otherNode)
otherNode
节点
with which to compare the first
node
’s document position.
An integer value whose bits represent the
otherNode
's relationship to the calling
node
.
More than one bit is set if multiple scenarios apply. For example, if
otherNode
is located earlier in the document
and
包含
node
on which
compareDocumentPosition()
was called, then both the
DOCUMENT_POSITION_CONTAINS
and
DOCUMENT_POSITION_PRECEDING
bits would be set, producing a value of 10 (
0x0A
).
const head = document.head;
const body = document.body;
if (head.compareDocumentPosition(body) & Node.DOCUMENT_POSITION_FOLLOWING) {
console.log('Well-formed document');
} else {
console.error('<head> is not before <body>');
}
注意:
Because the result returned by
compareDocumentPosition()
is a bitmask, the
bitwise AND operator
must be used for meaningful results.
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'Node.compareDocumentPosition()' in that specification. |
实时标准 | |
|
DOM (文档对象模型) 3 级核心规范
The definition of 'Node.compareDocumentPosition()' in that specification. |
过时 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
compareDocumentPosition
|
Chrome Yes | Edge 12 | Firefox 9 |
IE
9
|
Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 9 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
见实现注意事项。
节点
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