--- title: Document.createCDATASection() slug: Web/API/Document/createCDATASection tags: - API - DOM - 参考 - 方法 translation_of: Web/API/Document/createCDATASection ---
createCDATASection()
创建并返回一个新的 CDATA 片段节点。
var CDATASectionNode = document.createCDATASection(data);
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>
NOT_SUPPORTED_ERR
.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 ({{domxref("document.createTextNode","createTextNode()")}} can often be used in its place).规范 | 状态 | 备注 |
---|---|---|
{{SpecName('DOM WHATWG', '#dom-document-createcdatasection', 'document.createCDATASection')}} | {{Spec2('DOM WHATWG')}} |
{{Compat("api.Document.createCDATASection")}}