DOM Node物件屬性nodeValue

2019-10-16 23:20:00

DOM Node物件屬性nodeValue用於根據節點型別指定節點的值。

語法

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

nodeObject.nodeValue

範例
檔案: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>

以下範例演示了nodeValue屬性的用法,檔案:nodevalue.html -

<!DOCTYPE html>
<html>
   <body>
      <div>
         <b>FirstName:</b><span id = "FirstName"></span><br>
         <b>LastName:</b> <span id = "LastName"></span><br>

      </div>
      <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;

         document.getElementById("FirstName").innerHTML =
         xmlDoc.getElementsByTagName("FirstName")[0].childNodes[0].nodeValue;
         document.getElementById("LastName").innerHTML =
         xmlDoc.getElementsByTagName("LastName")[0].childNodes[0].nodeValue;

      </script>
   </body>
</html>

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