XML DOM nextSibling property

Definition and Usage

nextSibling This property returns the node that follows a certain node (the next node at the same tree level).

If there is no such node, this property returns null.

Syntax

nodeObject.nextSibling
Tips and Notes

Note:Firefox and most other browsers consider whitespace or line breaks as text nodes, while Internet Explorer does not. Therefore, in the following example, we use a function to check the node type of the next sibling node.

Element nodes' nodeType is 1, so if the next sibling node is not an element node, it moves to the next node and checks if the node is an element node. This continues until the next sibling node (must be an element node) is found. As a result, it is correct in all browsers.

İpucu:Tarayıcılar arasındaki farklılıklar hakkında daha fazla bilgi edinmek için XML DOM Eğitimindeki DOM Tarayıcı bölümüne gidin.

Örnek

Örnek 1

Aşağıdaki kod "books.xml" dosyasını xmlDoc içine yükler ve ilk <title> elementinden bir sonraki aynı düzeydeki düğümü alı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();
// Bir sonraki aynı düzeydeki kardeş düğümün element düğüm olup olmadığını kontrol et
function get_nextsibling(n) {
    var x = n.nextSibling;
    while (x.nodeType != 1) {
        x = x.nextSibling;
    }
    return x;
}
function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    var x = xmlDoc.getElementsByTagName("title")[0];
    var y = get_nextsibling(x);
    document.getElementById("demo").innerHTML = x.nodeName + " = " +
    x.childNodes[0].nodeValue +
    "<br>Next sibling: " + y.nodeName + " = " +
    y.childNodes[0].nodeValue;
}

Kişisel olarak deneyin

Örnek 2

节点的前一个同级节点获取:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myFunction(this);
    }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
// Denetle bir önceki aynı düzeydeki düğümün element düğüm olup olmadığını kontrol et
function get_previoussibling(n) {
    var x = n.previousSibling;
    while (x.nodeType != 1) {
        x = x.previousSibling;
    }
    return x;
}
function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    var x = xmlDoc.getElementsByTagName("author")[0];
    var y = get_previoussibling(x);
    document.getElementById("demo").innerHTML = x.nodeName + " = " + 
    x.childNodes[0].nodeValue +
    "<br>Önceki kardeş: " + y.nodeName + " = " + 
    y.childNodes[0].nodeValue;
}

Kişisel olarak deneyin