Dom-nodelist-length

提供:Dev Guides
移動先:案内検索

DOM-NodeListオブジェクト属性-長さ

属性_length_は、ノードリスト内のノードの数を示します。

構文

_length_属性を使用するための構文は次のとおりです。

nodelistObject.length

_node.xml_の内容は以下のとおりです-

<?xml version = "1.0"?>
<Company>
   <Employee category = "Technical">
      <FirstName>Tanmay</FirstName>
      <LastName>Patil</LastName>
      <ContactNo>1234567890</ContactNo>
      <Email>[email protected]</Email>
   </Employee>

   <Employee category = "Non-Technical">
      <FirstName>Taniya</FirstName>
      <LastName>Mishra</LastName>
      <ContactNo>1234667898</ContactNo>
      <Email>[email protected]</Email>
   </Employee>

   <Employee category = "Management">
      <FirstName>Tanisha</FirstName>
      <LastName>Sharma</LastName>
      <ContactNo>1234562350</ContactNo>
      <Email>[email protected]</Email>
   </Employee>
</Company>

次の例では、XMLドキュメント(link:/dom/node.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_としてサーバーパスに保存します(このファイルとnode.xmlはサーバーの同じパスにある必要があります)。 次のように出力を取得します-

Length of node list: 3