DOM - 处理指令对象属性 - data



data 属性是描述紧接在?>之前的应用程序要处理的信息的字符。

语法

以下是 data 属性用法的语法。

ProcessingInstruction.target

序号 参数和说明
1

data

它是描述紧接在?>之前的应用程序要处理的信息的字符。

示例

以下示例演示了 data 属性的用法:

<!DOCTYPE html>
<html>
   <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("First child is : ");
         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>

执行

以 dom_processinginstruction_data.htm 的形式将该文件保存在服务器路径上。我们将获得如下所示的输出:

PI demo
dom_processinginstruction_object.htm
广告