Document.createCDATASection()

翻译不完整。 请帮助我们翻译这篇文章!

createCDATASection() 创建并返回一个新的 CDATA 片段节点。

语法

var CDATASectionNode = document.createCDATASection(data);
  • CDATASectionNode 是一个 CDATA 片段节点。
  • data 包含了要被添加至 CDATA 片段的数据的字符串。

示例

var docu = new DOMParser().parseFromString('<xml></xml>', 'application/xml');

var cdata = docu.createCDATASection('Some <CDATA> data & then some');

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

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

备注

  • 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 NOT_SUPPORTED_ERR.
  • Will throw a NS_ERROR_DOM_INVALID_CHARACTER_ERR exception if one tries to submit the closing CDATA sequence ("]]>") as part of the data, so unescaped user-provided data cannot be safely used without with this method getting this exception (createTextNode() can often be used in its place).

规范

规范 状态 备注
DOM
document.createCDATASection
Living Standard

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
createCDATASectionChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 9Opera Full support ≤12.1Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support ≤12.1Safari iOS Full support 1Samsung Internet Android Full support 1.0

Legend

Full support  
Full support