DOM ProcessingInstruction物件target屬

2019-10-16 23:19:19

DOM ProcessingInstruction物件target屬標識指令或資料所針對的應用程式。

語法

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

ProcessingInstruction.target

範例

以下範例演示了target屬性的用法 -

<!DOCTYPE html>
<html>
   <meta charset="utf-8"/>
    <head>
      <script>
         // loads the xml string in a dom object
         function loadXMLString(t) {
            // for non IE browsers
            if (window.DOMParser) {
               // create an instance for xml dom object
               parser = new DOMParser();
               xmlDoc = parser.parseFromString(t,"text/xml");
            } else // code for IE 
            {
               // create an instance for xml dom object
               xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
               xmlDoc.async = false;
               xmlDoc.loadXML(t);
            }
            return xmlDoc;
         }
         function get_firstChild(p) {
            a = p.firstChild;
            return a;
         }
      </script>
   </head>
   <body>
      <script>
         var xml = "<Employee>";
         xml = xml+"<FirstName>";
         xml = xml+"<?piTarget piData more piData?>";
         xml = xml+"</FirstName>";

         xml = xml+"</Employee>";

         // calls the loadXMLString() with "text" function and store the xml dom in a variable
         var xmlDoc = loadXMLString(xml);

         var x = get_firstChild(xmlDoc.getElementsByTagName("FirstName")[0]);
         document.write("第一個節點是 : ");
         document.write(x.nodeName);

         //the following should be "piData more piData"
         alert(x.data);

         //the following should be "piTarget"
         alert(x.target);
      </script>
   </body>
</html>

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