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

XML DOM setAttributeNode() メソッド


Element Object Reference Element オブジェクト

定義と用法

setAttributeNode() メソッドは、属性ノードを追加します。

同じ名前を持つ属性がすでに要素に存在する場合、それは新しいもので置き換えられます。 新しい属性が既存の属性を置換する場合は置換される属性ノードを返し、それ以外の場合は null を返します。

構文

elementNode.setAttributeNode(att_node)

パラメータ 説明
att_node 必須。設定する属性ノードを指定


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

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('book');
var newatt;

for (i=0;i<x.length;i++)
{
newatt=xmlDoc.createAttribute("edition");
newatt.value="first";
x[i].setAttributeNode(newatt);
}

//Output all "edition" attribute values
for (i=0;i<x.length;i++)
{
document.write("Edition: ");
document.write(x[i].getAttribute("edition"));
document.write("<br />");
}

出力:

Edition: first
Edition: first
Edition: first
Edition: first

試してください »

Element Object Reference Element オブジェクト