XML DOM appendChild() 方法

定義和用法
appendChild() 方法把新的子節點追加到節點的子節點列表的末尾。
該方法返回新的子節點。
語法
appendChild(newchild)
參數 | 描述 |
---|---|
newchild | 要追加的節點。 |
實例
下麵的代碼片段使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中,創建節點(<edition>),並把它添加到第一個 <book> 節點的最後一個子節點的後面:
實例
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
document.write(x.getElementsByTagName("edition")[0].nodeName);
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
document.write(x.getElementsByTagName("edition")[0].nodeName);
輸出:
edition
嘗試一下
