DOM - Attributo oggetto nodo - nodeType

L'attributo nodeType è un codice che rappresenta il tipo di oggetto sottostante.

Sintassi

Di seguito è riportata la sintassi per l'utilizzo dell'attributo nodeType .

nodeObject.nodeType

Esempio

I contenuti di node.xml sono i seguenti:

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

L'esempio seguente dimostra l'utilizzo dell'attributo nodeType :

<!DOCTYPE html>
<html>
   <head>
      <script>
         function loadXMLDoc(filename) {
            if (window.XMLHttpRequest) {
               xhttp = new XMLHttpRequest();
            } else // code for IE5 and IE6 {
               xhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xhttp.open("GET",filename,false);
            xhttp.send();
            return xhttp.responseXML;
         }
      </script>
   </head>
   <body>
      <script>
         xmlDoc = loadXMLDoc("/dom/node.xml");

         document.write("Nodename: " + xmlDoc.nodeName);
         document.write(" (nodetype: " + xmlDoc.nodeType + ") <br>");

         x = xmlDoc.documentElement;

         document.write("Nodename: " + x.nodeName);
         document.write(" (nodetype: " + x.nodeType + ") <br>");
      </script>
   </body>
</html>

Esecuzione

Salvare questo file come nodeattribute_nodetype.htm nel percorso del server (questo file e node.xml dovrebbero trovarsi sullo stesso percorso nel server). Otterremo l'output come mostrato di seguito -

Nodename: #document (nodetype: 9)
Nodename: Company (nodetype: 1)