aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/document/createprocessinginstruction/index.html
blob: 7061666c12b5ac82b8457f04f070fc4f050f1c5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
---
title: Document.createProcessingInstruction()
slug: Web/API/Document/createProcessingInstruction
translation_of: Web/API/Document/createProcessingInstruction
---
<p id="Summary">{{APIRef("DOM")}}</p>

<p><code>createProcessingInstruction() </code>创建一个新的处理指令节点,并返回。</p>

<h2 id="Syntax" name="Syntax">Syntax</h2>

<pre class="eval"><em>Processing instruction node</em> = document.createProcessingInstruction(target, data)
</pre>

<h3 id="Parameters" name="Parameters">Parameters</h3>

<ul>
 <li><code>Processing Instruction node</code> 是 {{ domxref("ProcessingInstruction") }} 节点.</li>
 <li><code>target</code> 是指处理指令节点的target部分 (i.e., &lt;?<em>target</em> ... ?&gt;</li>
 <li><code>data</code> 是一个字符串,包含了将要添加到节点内的数据。</li>
</ul>

<h3 id="Notes" name="Notes">异常</h3>

<dl>
 <dt><code>NOT_SUPPORTED_ERR</code></dt>
 <dd>Thrown if you attempt to create a processing instruction node on an HTML document in Gecko 9 {{ geckoRelease("9.0") }} or earlier. In Gecko 10.0 {{ geckoRelease("10.0") }} and later, you can use this method on HTML documents.</dd>
 <dt><code>DOM_INVALID_CHARACTER</code></dt>
 <dd>Thrown if you try to add an invalid processing instruction target (it should be an XML name besides any case combination of the letters "xml") or if the closing processing instruction sequence ("?&gt;") is added as part of the data, so unescaped user-provided data cannot be safely used without escaping or otherwise dealing with such situations.</dd>
</dl>

<h2 id="Example" name="Example">实例</h2>

<pre>var docu = new DOMParser().parseFromString('&lt;xml&gt;&lt;/xml&gt;',  "application/xml")

var pi = docu.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"');

docu.insertBefore(pi, docu.firstChild);

alert(new XMLSerializer().serializeToString(docu));
// 弹出框内容: &lt;?xml-stylesheet href="mycss.css" type="text/css"?&gt;&lt;xml/&gt;
</pre>

<h2 id="Specification" name="Specification">规范</h2>

<p><a class="external" href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-document-createprocessinginstruction">DOM 4: createProcessingInstruction</a></p>