XML DOM appendChild() మార్గదర్శకం

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

appendChild() ఈ మార్గదర్శకం కొన్ని ఎలిమెంట్ నోడ్ యొక్క చివరి కుమార నోడ్ తర్వాత నోడ్ ను జోడిస్తుంది.

ఈ మార్గదర్శకం కొత్త కుమార నోడ్ ను తిరిగి ఇస్తుంది.

విధానం

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

ఉదాహరణ

ఉదాహరణ 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;
}

亲自试一试

ఉదాహరణ 2

ఈ కోడు "books.xml" ను xmlDoc కి లోడ్ చేస్తుంది మరియు కొత్త నోడ్స్ ను అన్ని <book> ఎలమెంట్స్ కు జోడిస్తుంది:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        myFunction(xhttp);
    }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
    var x, y, z, i, newel, newtext, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName("book");
    for (i = 0; i < x.length; i++) {
        newel = xmlDoc.createElement("edition");
        newtext = xmlDoc.createTextNode("first");
        newel.appendChild(newtext);
        x[i].appendChild(newel);
    }
    // అన్ని title మరియు edition నివేదించండి
    y = xmlDoc.getElementsByTagName("title");
    z = xmlDoc.getElementsByTagName("edition");
    for (i = 0; i < y.length; i++) {
        txt += y[i].childNodes[0].nodeValue +""
        " - Edition: " +
        z[i].childNodes[0].nodeValue + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

亲自试一试