XML DOM length 属性
XML基础 2023-08-13 22:19:22小码哥的IT人生shichen
XML DOM length 属性
定义和用法
length 属性可返回一个节点列表中的节点数目。
语法
nodelistObject.length
实例
在所有的例子中,我们将使用 XML 文件 books.xml,以及 JavaScript 函数 loadXMLDoc()。
下面的代码片段可取得 XML 文档 中<title> 元素的数目:
xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName('title');
document.write("Number of title elements: " + x.length
);
输出:
Number of title elements: 4
TIY
完整实例【length - 可返回节点列表中的节点数目】:
<html>
<head>
<script type="text/javascript" src="/demo/example/xdom/loadxmldoc.js">
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("/demo/example/xdom/books.xml");
var x=xmlDoc.getElementsByTagName('title');
document.write("Number of title elements: " + x.length);
</script>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
完整实例【length - 循环遍历节点列表中的节点】:
<html>
<head>
<script type="text/javascript" src="/demo/example/xdom/loadxmldoc.js">
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("/demo/example/xdom/books.xml");
var x=xmlDoc.getElementsByTagName('title');
for (i=0;i<x.length;i++)
{
document.write(x[i].childNodes[0].nodeValue);
document.write("<br />");
}
</script>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html