DOM - 命名節點對映物件專案 - length



屬性 length 給出了此對映中的節點數。有效的子節點索引範圍包括 0 到 length-1。

語法

以下是使用 length 屬性的語法。

nodemapObject.length

示例

node.xml 內容如下 −

<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>

以下示例演示瞭如何使用 length 屬性 −

<!DOCTYPE html>
<html>
   <body>
      <script>
         if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
         } else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.open("GET","/dom/node.xml",false);
         xmlhttp.send();
         xmlDoc = xmlhttp.responseXML;

         x = xmlDoc.getElementsByTagName("Employee");
         document.write("Length is : ");
         document.write(x.item(0).attributes.length);
      </script>
   </body>
</html>

執行

將該檔案另存為伺服器路徑上的 namednodemapproperty_length.htm (此檔案和 node_methods.xml 應位於伺服器上的同一路徑)。我們將獲得如下所示的輸出 −

Length is : 1
dom_namednodemap_object.htm
廣告
© . All rights reserved.