XML DOM length 属性
XML基础 2023-08-13 19:41:132小码哥的IT人生shichen
XML DOM length 属性
定义和用法
length 属性可返回 NamedNodeMap 中的节点数目。
语法
nodemapObject.length
实例
在所有的例子中,我们将使用 XML 文件 books.xml,以及 JavaScript 函数 loadXMLDoc()。
下面的代码片段可取得 XML 文档的第一个 <book> 元素中属性的数目:
xmlDoc=loadXMLDoc("books.xml"); var x=xmlDoc.getElementsByTagName('book'); document.write(x.item(0).attributes.length);
输出:
1
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('book'); document.write("Number of attributes in the first book element: "); document.write(x.item(0).attributes.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'); 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