XML DOM getAttribute() 方法

定義和用法

getAttribute() 方法通過名稱獲取屬性的值。

語法:

elementNode.getAttribute(name)
參數 描述
name 必需。規定從中取得屬性值的屬性。

提示和注釋

對于使用名稱空間的 XML 文檔來說,需要使用 getAttributeNS() 方法

實例

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

下面的代碼片段獲取所有 <book> 元素中 "category" 屬性的值:

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
for (i=0;i<x.length;i++)
{
document.write(x[i].getAttribute('category'));
document.write("<br />");
}

以上代碼的輸出:

COOKING
CHILDREN
WEB
WEB