XML DOM getAttribute() 方法

Element 对象参考手册

定义和用法

getAttribute() 方法通过名称获取属性的值。

语法:

elementNode.getAttribute(name)
参数 描述
name 必需。规定从中取得属性值的属性。
本在线速查手册由www.w`3`x`u`e.com提供,请勿盗用!

提示和注释

对于使用名称空间的 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

TIY

getAttribute() - 获取指定属性的值

Element 对象参考手册