ホーム HTML CSS XML JAVASCRIPT   PHP SQL MORE...   リファレンス 事例集    

XML DOM appendChild() メソッド


Element Object Reference Element オブジェクト

定義と用法

appendChild() メソッドは、指定した要素ノードの最後の子ノードの後ろにノードを追加します。

このメソッドは、新しい子ノードを返します。

構文

appendChild(node)

パラメータ 説明
node 必須。追加するノード


例 1

次のコードは、loadXMLDoc() を使用して xmlDoc に "books.xml" をロードし、 要素 (<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);

出力:

edition

試してください »

例 2

次のコードは、loadXMLDoc() を使用して xmlDoc に "books.xml" をロードし、 全 <book> 要素へ新規ノードを追加します:

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('book');
var newel,newtext;

for (i=0;i<x.length;i++)
{
newel=xmlDoc.createElement('edition');
newtext=xmlDoc.createTextNode('First');
newel.appendChild(newtext);
x[i].appendChild(newel);
}

//Output all titles and editions
y=xmlDoc.getElementsByTagName("title");
z=xmlDoc.getElementsByTagName("edition");
for (i=0;i<y.length;i++)
{
document.write(y[i].childNodes[0].nodeValue);
document.write(" - Edition: ");
document.write(z[i].childNodes[0].nodeValue);
document.write("<br />");
}

出力:

Everyday Italian - Edition: First
Harry Potter - Edition: First
XQuery Kick Start - Edition: First
Learning XML - Edition: First

試してください »

Element Object Reference Element オブジェクト