एक्सएमएल डोम रिप्लेसचाइल्ड () विधि
नोड वस्तु
उदाहरण
निम्न कोड खंड " 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;
// Create a book
element, title element and a text node
newNode = xmlDoc.createElement("book");
newTitle = xmlDoc.createElement("title");
newText = xmlDoc.createTextNode("A
Notebook");
// Add a text node to the title node
newTitle.appendChild(newText);
// Add the title node to the book node
newNode.appendChild(newTitle);
y = xmlDoc.getElementsByTagName("book")[0];
// Replace the first book node with the new book node
x.replaceChild(newNode,
y);
z = xmlDoc.getElementsByTagName("title");
// Output all titles
for (i = 0; i < z.length; i++) {
txt += z[i].childNodes[0].nodeValue
+ "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
उपरोक्त कोड का आउटपुट होगा:
A Notebook
Harry Potter
XQuery Kick Start
Learning XML
परिभाषा और उपयोग
रिप्लेसचाइल्ड () विधि चाइल्ड नोड को नए नोड से बदल देती है।
नया नोड दस्तावेज़ में एक मौजूदा नोड हो सकता है, या आप एक नया नोड बना सकते हैं।
युक्ति: बदले गए चाइल्ड नोड को बाद में उसी दस्तावेज़ में किसी भी तत्व में डाला जा सकता है। बाद में उसी दस्तावेज़ में सम्मिलित करने के लिए insertBefore ( ) या appendChild ( ) विधि का उपयोग करें, या किसी अन्य दस्तावेज़ में प्रतिस्थापित नोड को सम्मिलित करने के लिए अपनाने नोड () या आयात नोड () विधि का उपयोग करें।
ब्राउज़र समर्थन
रिप्लेसचाइल्ड () विधि सभी प्रमुख ब्राउज़रों में समर्थित है।
वाक्य - विन्यास
nodeObject.replaceChild(newchild,oldchild)
मापदंडों
Parameter | Type | Description |
---|---|---|
newchild | Node object | Required. The new node to put in the child list |
oldchild | Node object | Required. The node that will be replaced in the child list |
प्रतिलाभ की मात्रा
Type | Description |
---|---|
Node object | The replaced node (oldchild) |
टेक्निकल डिटेल
डोम संस्करण | कोर स्तर 1 नोड वस्तु। डोम स्तर 3 . में संशोधित |
---|
नोड वस्तु