亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

XML DOM replaceChild() 方法

Node 對象參考手冊

定義和用法

replaceChild() 方法可將某個子節(jié)點替換為另一個。

如替換成功,此方法可返回被替換的節(jié)點,如替換失敗,則返回 NULL。。

語法:

nodeObject.replaceChild(new_node,old_node)
參數(shù) 描述
new_node 必需。指定新的節(jié)點。
old_node 必需。指定被替換的節(jié)點。

提示和注釋

注釋:Internet Explorer 會忽略節(jié)點間生成的空白文本節(jié)點(例如,換行符號),而 Mozilla 不會這樣做。因此,在下面的例子中,我們會使用一個函數(shù)來檢查首個子節(jié)點的節(jié)點類型。

元素節(jié)點的節(jié)點類型是 1,因此如果首個子節(jié)點不是一個元素節(jié)點,它就會移至下一個節(jié)點,然后繼續(xù)檢查此節(jié)點是否為元素節(jié)點。整個過程會一直持續(xù)到首個元素子節(jié)點被找到為止。通過這個方法,我們就可以在 Internet Explorer 和 Mozilla 得到正確的方法。

提示:如需更多有關(guān) IE 與 Mozilla 瀏覽器之間 XML DOM 的差異的內(nèi)容,請訪問我們的 DOM 瀏覽器 章節(jié)。

實例

在所有的例子中,我們將使用 XML 文件 books.xml,以及 JavaScript 函數(shù) loadXMLDoc()。

下面的代碼片段可替換首個 <book< 元素中的 <title> 元素:

//check if first child node is an element node
function get_firstchild(n)
{
var x=n.firstChild;
while (x.nodeType!=1)
  {
  x=x.nextSibling;
  }
return x;
}

xmlDoc=loadXMLDoc("books.xml");

//create a title element and a text node
var newNode=xmlDoc.createElement("title");
var newText=xmlDoc.createTextNode("Giada's Family Dinners");
//add the text node to the title node,
newNode.appendChild(newText);

//replace the first child node with the new node
var x=xmlDoc.getElementsByTagName("book")[0];
x.replaceChild(newNode,get_firstchild(x));

//output all titles
var 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

Node 對象參考手冊