XML DOM appendChild() మాధ్యమం

నిర్వచనం మరియు వినియోగం

appendChild() ఈ మాధ్యమం కొత్త కుమార నోడ్ ను నోడ్ యొక్క కుమార నోడ్ జాబితాలో చివరికి జోడిస్తుంది.

గమనిక:ఉంటే newchild ఇది చెట్టులో ఉన్నట్లయితే, ముందుగా దానిని తొలగించండి.

సింథాక్సిస్

nodeObject.appendChild(newchild)
పారామీటర్స్ వివరణ
newchild జోడించవలసిన నోడ్.

టెక్నికల్ వివరణ

DOM వెర్షన్: కోర్ లెవల్ 1 నోడ్ అబ్జెక్ట్. DOM లెవల్ 3 లో సవరించబడింది.
ఫలితం: నోడ్ అబ్జెక్ట్. జోడించబడిన నోడ్.

ఉదాహరణ

ఉదాహరణ 1

ఈ కోడు "books.xml" ని xmlDoc లోకి లోడ్ చేస్తుంది, మరియు ఒక నోడ్ ను (<edition>) సృష్టిస్తుంది, దానిని మొదటి <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 newel = xmlDoc.createElement("edition");
    var x = xmlDoc.getElementsByTagName("book")[0];
    x.appendChild(newel);
    document.getElementById("demo").innerHTML =
    x.getElementsByTagName("edition")[0].nodeName;
}

亲自试一试

Example 2

Append child nodes to all <book> nodes:

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 x, y, z, i, xLen, yLen, newEle, newText, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName("book");
    xLen = x.length;
    for (i = 0; i < xLen; i++) { 
        newEle = xmlDoc.createElement("edition");
        newText = xmlDoc.createTextNode("first");
        newEle.appendChild(newText);
        x[i].appendChild(newEle);
    }
    // Output all title and edition
    y = xmlDoc.getElementsByTagName("title");
    yLen = y.length
    z = xmlDoc.getElementsByTagName("edition");
    for (i = 0; i < yLen; i++) { 
        txt += y[i].childNodes[0].nodeValue + 
        " - Edition: " + 
        z[i].childNodes[0].nodeValue + "<br>";
    }
    document.getElementById("demo").innerHTML = txt; 
}

亲自试一试

浏览器支持

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
支持 支持 支持 支持 支持

所有主流浏览器都支持 appendChild() 方法。