XML DOM karşılaştırmaDocumentPosition() Yöntemi
❮ Düğüm Nesnesi
Örnek
Aşağıdaki kod parçası, " books.xml " dosyasını xmlDoc'a yükler ve DOM hiyerarşisindeki iki düğümün (birinci ve üçüncü <book> öğesi) yerleşimini karşılaştırır:
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);
}
Yukarıdaki kodun çıktısı şöyle olacaktır:
4
Çoğu tarayıcı, boş beyaz boşlukları veya yeni satırları metin düğümleri olarak değerlendirir, IE 9 ve önceki sürümler bunu yapmaz. Bu nedenle, yukarıdaki örnekte, çoğu tarayıcı 4 çıktı verirken, IE 9 ve önceki sürümler 2 çıktı verecektir.
Tanım ve Kullanım
karşılaştırmaDocumentPosition() yöntemi, DOM hiyerarşisindeki (belge) iki düğümün yerleşimini karşılaştırır.
Tarayıcı Desteği
CompareDocumentPosition() yöntemi, tüm büyük tarayıcılarda desteklenir.
Not: Internet Explorer 9 ve önceki sürümleri bu yöntemi desteklemez.
Sözdizimi
nodeObject.compareDocumentPosition(node)
parametreler
Parameter | Type | Description |
---|---|---|
node | Node object | Required. Specifies the node to compare with the current node |
Geri dönüş değeri
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). |
Teknik detaylar
DOM Sürümü | Çekirdek Düzey 3 Düğüm Nesnesi |
---|
❮ Düğüm Nesnesi