HTML DOM रिप्लेसचाइल्ड () मेथड
उदाहरण
सूची में <li> तत्व में टेक्स्ट नोड को नए टेक्स्ट नोड से बदलें:
// Create a new text node called "Water"
var textnode = document.createTextNode("Water");
// Get the first child node of an <ul> element
var item = document.getElementById("myList").childNodes[0];
// Replace the first child node of <ul> with the newly created text node
item.replaceChild(textnode, item.childNodes[0]);
// Note: This example replaces only the Text node "Coffee" with a Text node "Water"
बदलने से पहले:
- Coffee
- Tea
- Milk
बदलने के बाद:
- Water
- Tea
- Milk
नीचे और अधिक "इसे स्वयं आज़माएं" उदाहरण।
परिभाषा और उपयोग
रिप्लेसचाइल्ड () विधि चाइल्ड नोड को नए नोड से बदल देती है।
नया नोड दस्तावेज़ में एक मौजूदा नोड हो सकता है, या आप एक नया नोड बना सकते हैं।
युक्ति: किसी तत्व से चाइल्ड नोड को निकालने के लिए removeChild () विधि का उपयोग करें ।
ब्राउज़र समर्थन
Method | |||||
---|---|---|---|---|---|
replaceChild() | Yes | Yes | Yes | Yes | Yes |
वाक्य - विन्यास
node.replaceChild(newnode, oldnode)
पैरामीटर मान
Parameter | Type | Description |
---|---|---|
newnode | Node object | Required. The node object you want to insert |
oldnode | Node object | Required. The node object you want to remove |
टेक्निकल डिटेल
प्रतिलाभ की मात्रा: | एक नोड वस्तु, प्रतिस्थापित नोड का प्रतिनिधित्व करती है |
---|---|
डोम संस्करण | कोर स्तर 1 नोड वस्तु |
और ज्यादा उदाहरण
उदाहरण
सूची में <li> तत्व को नए <li> तत्व से बदलें:
// Create a new <li> element
var elmnt = document.createElement("li");
// Create a new text node called "Water"
var textnode = document.createTextNode("Water");
// Append the text node to <li>
elmnt.appendChild(textnode);
// Get the <ul> element with id="myList"
var item = document.getElementById("myList");
// Replace the first child node (<li> with index 0) in <ul> with the newly created <li> element
item.replaceChild(elmnt, item.childNodes[0]);
// Note: This example replaces the entire <li> element
हटाने से पहले:
- Coffee
- Tea
- Milk
हटाने के बाद:
- Water
- Tea
- Milk