---
title: CDATASection
slug: Web/API/CDATASection
tags:
  - API
  - DOM
  - 参考
  - 接口
translation_of: Web/API/CDATASection
---
<div>{{APIRef("DOM")}}</div>

<p><code><strong>CDATASection</strong></code> 接口用于表示 CDATA 片段(CDATA section)。在 XML 中, CDATA 可以直接包含未经转义的文本。比如 <code>&lt;</code> 和 <code>&amp;</code>,只要位于 CDATA 片段中,它们就不需要被转义,保持原样就可以了。</p>

<p>在 XML 中,一个 CDATA 片段格式如下:</p>

<pre class="syntaxbox">&lt;![CDATA[  ... ]]&gt;
</pre>

<p>例如:</p>

<pre class="brush:xml">&lt;foo&gt;这是一个CDATA section: &lt;![CDATA[  &lt; &gt; &amp; ]]&gt; 其中包含了一些没有转义的字符。 &lt;/foo&gt;
</pre>

<p>唯一的例外就是,在一个 CDATA 片段中不可以使用  CDATA 片段本身的闭合标签片段:</p>

<pre class="brush:xml">&lt;![CDATA[  ]]&gt; 会引发错误   ]]&gt;
</pre>

<p>注意,CDATA 片段不应该在 HTML 中被使用;它只在 XML 中有效。</p>

<p>{{InheritanceDiagram(600, 120)}}</p>

<h2 id="属性">属性</h2>

<p>这个接口没有特有的属性,但实现了父接口<em> {{domxref("Text")}}</em> 的属性。</p>

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

<p>这个接口没有特有的方法,但实现了父接口<em> {{domxref("Text")}}</em> 的方法。</p>

<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", "#interface-cdatasection", "CDATASection")}}</td>
   <td>{{Spec2("DOM WHATWG")}}</td>
   <td>Re-added in <a href="https://github.com/whatwg/dom/pull/295">issue #295</a> due to web breakage</td>
  </tr>
  <tr>
   <td>{{SpecName("DOM4", "#cdatasection", "CDATASection")}}</td>
   <td>{{Spec2("DOM4")}}</td>
   <td>Removed in favour of the more generic {{DOMxRef("Text")}} interface</td>
  </tr>
  <tr>
   <td>{{SpecName("DOM3 Core", "core.html#ID-667469212", "CDATASection")}}</td>
   <td>{{Spec2("DOM3 Core")}}</td>
   <td>No change from {{SpecName("DOM2 Core")}}</td>
  </tr>
  <tr>
   <td>{{SpecName("DOM2 Core", "core.html#ID-667469212", "CDATASection")}}</td>
   <td>{{Spec2("DOM2 Core")}}</td>
   <td>No change from {{SpecName("DOM1")}}.</td>
  </tr>
  <tr>
   <td>{{SpecName("DOM1", "level-one-core.html#ID-667469212", "CDATASection")}}</td>
   <td>{{Spec2("DOM1")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

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



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