XML DOM replaceChild() 方法
定义和用法
replaceChild()}
यह फ़ंक्शन एक उप-नोड को दूसरे उप-नोड से बदलता है。
सफल होने पर इस फ़ंक्शन से बदले गए नोड वापस दिया जाता है, असफल होने पर इसे वापस दिया जाता है NULL
।
व्याकरण
elementNode.replaceChild(new_node,old_node)
पारामीटर | वर्णन |
---|---|
new_node | अनिवार्य。 |
old_node | अनिवार्य。 |
उदाहरण
नीचे दिए गए कोड "books.xml" को xmlDoc में लोड करने और पहले <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, newNode, newTitle, newText, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.documentElement; // एक book एलीमेंट, title एलीमेंट और टेक्स्ट नोड बनाएं newNode = xmlDoc.createElement("book"); newTitle = xmlDoc.createElement("title"); newText = xmlDoc.createTextNode("Hello World"); // title नोड में एक टेक्स्ट नोड जोड़ें newTitle.appendChild(newText); // इस title नोड को book नोड में जोड़ें newNode.appendChild(newTitle); y = xmlDoc.getElementsByTagName("book")[0]; // इस नए book नोड को पहले book नोड के स्थान पर बदलें x.replaceChild(newNode, y); z = xmlDoc.getElementsByTagName("title"); // सभी title निकालें for (i = 0; i < z.length; i++) { txt += z[i].childNodes[0].nodeValue + "<br>"; } document.getElementById("demo").innerHTML = txt; }