DOM - Méthode d'objet NamedNodeMap - item ()

La méthode item () retourne l'élément d'index dans la carte. Si l'index est supérieur ou égal au nombre de nœuds de cette carte, cela renvoie null.

Syntaxe

Voici la syntaxe pour l'utilisation de la méthode item () .

nodemapObject.item(index)

S.No. Paramètre et description
1

index

Il spécifie la position de l'élément dans la carte. Il est de type non signé long .

Cette méthode renvoie l'élément d'index de la carte.

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 méthode item () -

<!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');


         item_name = x.item(0).attributes.getNamedItem("category");
         document.write("Get the specified item value : ")
         document.write( item_name.value );
     </script>
   </body>
</html>

Exécution

Enregistrez ce fichier sous namednodemapmethod_item.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 -

Get the specified item value : Technical