XML DOM replaceChild() method
Definition and Usage
replaceChild() method uses another node to replace a child node.
If successful, this method returns the replaced node, otherwise returns null.
Syntax:
elementNode.replaceChild(new_node,old_node)
parameters | bayanai |
---|---|
new_node | Zaɓa. Kama ɗan tashiya yana samuwa. |
old_node | Zaɓa. Kama ɗan tashiya yana samuwa. |
shaidari
A gaba daya, za a gina koyarwaki a fiila XML books.xmlda kuma functions na JavaScript loadXMLDoc()。
Duba kodayake haka na code a ɗan <book> na farko a ɗan <title> a "books.xml":
//kama ɗan farko ce ɗan ɗaya
function get_firstchild(n)
{
x=n.firstChild;
while (x.nodeType!=1)
{
x=x.nextSibling;
}
return x;
}
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0];
//kama ɗan tashiya tsa titibin da tashiya ɗan gudanarwa
newNode=xmlDoc.createElement("title");
newText=xmlDoc.createTextNode("Giada's Family Dinners");
//kama ɗan tashiya tsa titibin
newNode.appendChild(newText);
//replace the last node with the new node
x.replaceChild(newNode,get_firstchild(x));
y=xmlDoc.getElementsByTagName("title");
for (i=0;i<y.length;i++)
{
document.write(y[i].childNodes[0].nodeValue);
document.write("<br />");
}
输出:
Giada's Family Dinners Harry Potter XQuery Kick Start Learning XML
注释:Internet Explorer 会忽略节点间生成的空白文本节点(例如,换行符号),而 Mozilla 不会这样做。因此,在上面的例子中,我们创建了一个函数来创建正确的子元素。
提示:如需更多有关 IE 与 Mozilla 浏览器差异的内容,请访问 CodeW3C.com 的 XML DOM 教程中的 DOM 浏览器 这一节。