DOM NamedNodeMap物件length屬性

2019-10-16 23:19:35

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

語法

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

nodemapObject.length

範例

檔案:node.xml 的內容如下 -

<Company> 
   <Employee category = "Technical" id = "firstelement"> 
      <FirstName>Susen</FirstName> 
      <LastName>Su</LastName> 
      <ContactNo>1584567890</ContactNo> 
      <Email>[email protected]</Email> 
   </Employee>  

   <Employee category = "Non-Technical"> 
      <FirstName>Max</FirstName> 
      <LastName>Su</LastName> 
      <ContactNo>1334667898</ContactNo> 
      <Email>[email protected]</Email> 
   </Employee>  

   <Employee category = "Management"> 
      <FirstName>Min</FirstName> 
      <LastName>Su</LastName> 
      <ContactNo>1364562350</ContactNo> 
      <Email>[email protected]</Email> 
   </Employee> 
</Company>

以下範例演示了length 屬性的用法 -

<!DOCTYPE html>
<html>
   <meta charset="utf-8"/>
   <head></head>
   <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","/node.xml",false);
         xmlhttp.send();
         xmlDoc = xmlhttp.responseXML;

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

執行上面範例程式碼,得到以下結果 -