DOM - NodeList 对象属性 - length



属性length给出了节点列表中的节点数量。

语法

以下length属性使用情况的语法。

nodelistObject.length

示例

node.xml内容如下−

<?xml version = "1.0"?>
<Company>
   <Employee category = "Technical">
      <FirstName>Tanmay</FirstName>
      <LastName>Patil</LastName>
      <ContactNo>1234567890</ContactNo>
      <Email>tanmaypatil@xyz.com</Email>
   </Employee>
   
   <Employee category = "Non-Technical">
      <FirstName>Taniya</FirstName>
      <LastName>Mishra</LastName>
      <ContactNo>1234667898</ContactNo>
      <Email>taniyamishra@xyz.com</Email>
   </Employee>
   
   <Employee category = "Management">
      <FirstName>Tanisha</FirstName>
      <LastName>Sharma</LastName>
      <ContactNo>1234562350</ContactNo>
      <Email>tanishasharma@xyz.com</Email>
   </Employee>
</Company>

以下示例将 XML 文档 (node.xml) 解析为 XML DOM 对象,并使用 length 属性提取长度信息。

<!DOCTYPE html>
   <body>
      <script>
         if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
         } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.open("GET","/dom/node.xml",false);
         xmlhttp.send();
         xmlDoc = xmlhttp.responseXML;

         y = xmlDoc.getElementsByTagName('FirstName');
         document.write("Length of node list: " + y.length);
      </script>
   </body>
</html>

执行

将此文件另存为nodeattribute_length.htm(此文件和 node.xml 应在服务器中的同一路径上)。我们会得到如下输出−

Length of node list: 3
dom_nodelist_object.htm
广告
© . All rights reserved.