MDN wants to learn about developers like you: https://qsurvey.mozilla.com/s3/MDN-dev-survey

현재 번역은 완벽하지 않습니다. 한국어로 문서 번역에 동참해주세요.

Node.compareDocumentPosition() 메서드는 현재 노드와 문서 안의 다른 노드와의 위치를 비교한다.

반환 값은 아래의 값을 가지는 bitmask이다 :

Name Value
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

Syntax

node.compareDocumentPosition( otherNode ) 

Example

var head = document.getElementsByTagName('head').item(0);

if (head.compareDocumentPosition(document.body) & Node.DOCUMENT_POSITION_FOLLOWING) {
  console.log("well-formed document");
} else {
  console.log("<head> is not before <body>");
}

Note: Because the result returned by compareDocumentPosition is a bitmask, the bitwise and operator has to be used for meaningful results.

Note: First statement uses NodeList method item(0), which is equivalent to getElementsByTagName('head')[0].

Specifications

Specification Status Comment
DOM
The definition of 'Node.compareDocumentPosition()' in that specification.
Living Standard  
Document Object Model (DOM) Level 3 Core Specification
The definition of 'Node.compareDocumentPosition()' in that specification.
Recommendation Initial definition

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) 9.0 (Yes) (Yes)
Feature Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) ? (Yes) (Yes)

See also

문서 태그 및 공헌자

태그: 
 이 페이지의 공헌자: khw1031
 최종 변경: khw1031,