dd

XML DOM length 属性


定义和用法

length 属性返回 NamedNodeMap 中节点的数量。

语法

nodemapObject.length

实例

下面的代码片段使用 loadXMLDoc() 把 "books.xml" 载入 xmlDoc 中,并取得第一个 <book> 元素中属性的数量:

实例

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('book');

document.write(x.item(0).attributes.length);

上面的代码将输出:

1
 

尝试一下 Demos

length - 返回节点列表中的节点数量

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"> 
</script>
</head>
<body>
<script>
xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('title');

document.write("Number of title elements: " + x.length);
</script>
</body>
</html>

length - 循环遍历节点列表中的节点

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"> 
</script>
</head>
<body>
<script>
xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('title');

for (i=0;i<x.length;i++)
  {
  document.write(x[i].childNodes[0].nodeValue);
  document.write("<br>");
  }

</script>
</body>
</html>