XML DOM appendChild() मेथड
परिभाषा और उपयोग
appendChild()
यह मेथड नए सह-उप-आयाम को नोड के सह-उप-आयाम सूची के अंत में जोड़ता है।
ध्यान:यदि newchild अगर इसे पेड़ में पाया गया तो पहले इसे मिटाया जाएगा।
व्याकरण
nodeObject.appendChild(newchild)
पारामीटर | वर्णन |
---|---|
newchild | जोड़ने वाले नोड |
तकनीकी विवरण
DOM संस्करण: | Core Level 1 Node Object। DOM Level 3 में संशोधित। |
---|---|
परिणाम: | Node ऑब्जेक्ट। जोड़े गए नोड। |
उदाहरण
उदाहरण 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
सभी <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 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); } // सभी title और edition निकालें y = xmlDoc.getElementsByTagName("title"); yLen = y.length z = xmlDoc.getElementsByTagName("edition"); for (i = 0; i < yLen; i++) { txt += y[i].childNodes[0].nodeValue + " - संस्करण: " + z[i].childNodes[0].nodeValue + "<br>"; } document.getElementById("demo").innerHTML = txt; }
ब्राउज़र समर्थन
च्रोम | एज | फ़ायरफॉक्स | सैफारी | ऑपेरा |
---|---|---|---|---|
च्रोम | एज | फ़ायरफॉक्स | सैफारी | ऑपेरा |
समर्थन | समर्थन | समर्थन | समर्थन | समर्थन |
सभी मुख्य ब्राउज़र से appendChild() विधि का समर्थन करते हैं。