XML DOM attributes 屬性

定義和用法
attributes 屬性返回包含被選節點屬性的 NamedNodeMap。
如果被選節點不是元素,則該屬性返回 NULL。
語法
elementNode.attributes
提示和注釋
提示:該屬性僅用於 element 節點。
實例 1
下麵的代碼片段使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中,並取得 "books.xml" 中第一個 <title> 元素的屬性的數量:
實例
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0].attributes;
document.write(x.length);
x=xmlDoc.getElementsByTagName("book")[0].attributes;
document.write(x.length);
上面的代碼將輸出:
1
實例 2
下麵的代碼片段使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中,並取得第一個 <book> 元素中 "category" 屬性的值:
實例
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0].attributes;
att=x.getNamedItem("category");
document.write(att.value);
x=xmlDoc.getElementsByTagName("book")[0].attributes;
att=x.getNamedItem("category");
document.write(att.value);
上面的代碼將輸出:
COOKING
