replaceChild()
Misol
<li> elementidagi matn tugunini yangi matn tuguni bilan almashtiring:
const newNode = document.createTextNode("Water");
const element = document.getElementById("myList").children[0];
element.replaceChild(newNode, element.childNodes[0]);
Tavsif
replaceChild() metodi bola tugunni yangi tugun bilan almashtiradi.
Shuningdek qarang:
Tegishli Document metodlari:
Sintaksis
node.replaceChild(newnode, oldnode)
Parametrlar
| Parametr | Tavsif |
| newnode | Majburiy. Joylashtiriladigan tugun. |
| oldnode | Majburiy. Olib tashlanadigan tugun. |
Qaytariladigan qiymat
| Tip | Tavsif |
| Node | Almashtirilgan tugun. |
Ko‘proq misollar
Misol
<li> elementini yangi <li> elementi bilan almashtiring:
// Create a new <li> element:
const element = document.createElement("li");
// Create a new text node:
const textNode = document.createTextNode("Water");
// Append the text node to the <li> element:
element.appendChild(textNode);
// Get the <ul> element with id="myList":
const list = document.getElementById("myList");
// Replace the first child node with the new <li> element:
list.replaceChild(element, list.childNodes[0]);
Oldin:
- Coffee
- Tea
- Milk
Keyin:
- Water
- Tea
- Milk
Brauzerlarda qo‘llab-quvvatlash
element.replaceChild() — DOM Level 1 (1998) imkoniyati.
U barcha brauzerlarda to‘liq qo‘llab-quvvatlanadi:
| Chrome | Edge | Firefox | Safari | Opera | IE |
| Ha | Ha | Ha | Ha | Ha | 9-11 |
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
