XML DOM compareDocumentPosition() 메서드
❮ 노드 객체
예시
다음 코드 조각은 " books.xml "을 xmlDoc에 로드하고 DOM 계층 구조에서 두 노드(첫 번째 및 세 번째 <book> 요소)의 배치를 비교합니다.
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var x =
xmlDoc.getElementsByTagName('book')[0];
var y =
xmlDoc.getElementsByTagName('book')[2];
document.getElementById("demo").innerHTML =
x.compareDocumentPosition(y);
}
위 코드의 출력은 다음과 같습니다.
4
대부분의 브라우저는 빈 공백이나 새 줄을 텍스트 노드로 처리하지만 IE 9 및 이전 버전은 처리하지 않습니다. 따라서 위의 예에서 대부분의 브라우저는 4를 출력하는 반면 IE 9 및 이전 버전은 2를 출력합니다.
정의 및 사용
compareDocumentPosition() 메서드는 DOM 계층(문서)에서 두 노드의 배치를 비교합니다.
브라우저 지원
compareDocumentPosition() 메서드는 모든 주요 브라우저에서 지원됩니다.
참고: Internet Explorer 9 및 이전 버전은 이 방법을 지원하지 않습니다.
통사론
nodeObject.compareDocumentPosition(node)
매개변수
Parameter | Type | Description |
---|---|---|
node | Node object | Required. Specifies the node to compare with the current node |
반환 값
Type | Description |
---|---|
Number | A Number representing where the two nodes are positioned compared to each other. The possible return values
are:
1 - No relationship, the two nodes do not belong to the same document. 2 - The specified node precedes the current node. 4 - The specified node follows the current node. 8 - The specified node contains the current node. 16 - The specified node is contained by the current node. 32 - The specified and the current node have no common container node or the two nodes are different attributes of the same node. Note: The return value could also be a combination of values. E.g. a return value of 20 means that the specified node is contained by the current node (16) AND the specified node follows the current node (4). |
기술적 세부 사항
DOM 버전 | 코어 레벨 3 노드 객체 |
---|
❮ 노드 객체