aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/document/createcdatasection/index.html
blob: 298e0021fbbe1935e2bcc927d4d854876d3ab273 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
---
title: Document.createCDATASection()
slug: Web/API/Document/createCDATASection
tags:
  - API
  - DOM
  - 参考
  - 方法
translation_of: Web/API/Document/createCDATASection
---
<div>{{APIRef("DOM")}}</div>

<p><strong><code>createCDATASection()</code></strong> 创建并返回一个新的 CDATA 片段节点。</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">var <var>CDATASectionNode</var> = <var>document</var>.createCDATASection(<var>data</var>);
</pre>

<ul>
 <li><var>CDATASectionNode</var> 是一个 <a href="/en-US/docs/Web/API/CDATASection">CDATA 片段</a>节点。</li>
 <li><var>data</var> 包含了要被添加至 CDATA 片段的数据的字符串。</li>
</ul>

<h2 id="示例">示例</h2>

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

var cdata = docu.createCDATASection('Some &lt;CDATA&gt; data &amp; then some');

docu.getElementsByTagName('xml')[0].appendChild(cdata);

alert(new XMLSerializer().serializeToString(docu));
// Displays: &lt;xml&gt;&lt;![CDATA[Some &lt;CDATA&gt; data &amp; then some]]&gt;&lt;/xml&gt;
</pre>

<h2 id="备注">备注</h2>

<ul>
 <li>This will only work with XML, not HTML documents (as HTML documents do not support CDATA sections); attempting it on an HTML document will throw <code>NOT_SUPPORTED_ERR</code>.</li>
 <li>Will throw a <code>NS_ERROR_DOM_INVALID_CHARACTER_ERR</code> exception if one tries to submit the closing CDATA sequence ("<code>]]&gt;</code>") as part of the data, so unescaped user-provided data cannot be safely used without with this method getting this exception ({{domxref("document.createTextNode","createTextNode()")}} can often be used in its place).</li>
</ul>

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

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('DOM WHATWG', '#dom-document-createcdatasection', 'document.createCDATASection')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.Document.createCDATASection")}}</p>