English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM compareDocumentPosition() Method

HTML DOM Element Object

MethodThe method is to compare the position of the given node with any other node in any document.

The return value is a bitmask with the following values:

NameValues
DOCUMENT_POSITION_DISCONNECTED1
DOCUMENT_POSITION_PRECEDING2
DOCUMENT_POSITION_FOLLOWING4
DOCUMENT_POSITION_CONTAINS8
DOCUMENT_POSITION_CONTAINED_BY16
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC32

Syntax:

node.compareDocumentPosition(node)
Example1 var p1= document.getElementById("p
Example2 var p2= document.getElementById("p
").lastChild;1var x = p2.compareDocumentPosition(p
);/Test and see‹

Browser Compatibility

All browsers fully support the compareDocumentPosition() method:
MethodcompareDocumentPosition()compareDocumentPosition()compareDocumentPosition()compareDocumentPosition()compareDocumentPosition()

Yes

Parameter ValueParameter
Descriptionnode

Specify the node to be compared with the current node

Technical DetailsReturn value:

A number representing the position between the two nodes.

1Possible return values:

2).Is located before the second node (p1).Is located after the first node (p2).There is no relationship, the two nodes do not belong to the same document.

4).Is located before the second node (p1).Is located after the first node (p2).Is located after the second node (p

8).Is located before the first node (p1).Is located at the second node (p2).Inside.

16).The second node (p2).Is located at the first node (p1).Inside.

32There is no relationship, or two nodes are two attributes on the same element.

DOM Version:DOM Level1

HTML DOM Element Object