DOM - Projet d'objet NamedNodeMap - longueur

La longueur de la propriété donne le nombre de nœuds dans cette carte. La plage des indices de nœud enfant valides va de 0 à longueur-1 inclus.

Syntaxe

Voici la syntaxe pour l'utilisation de la propriété length .

nodemapObject.length

Exemple

Le contenu de node.xml est comme ci-dessous -

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

L'exemple suivant montre l'utilisation de la propriété 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>

Exécution

Enregistrez ce fichier sous namednodemapproperty_length.htm sur le chemin du serveur (ce fichier et node_methods.xml doivent être sur le même chemin sur votre serveur). Nous obtiendrons la sortie comme indiqué ci-dessous -

Length is : 1