Document.createCDATASection()
åºçº¿
广æ³å¯ç¨
èª 2015å¹´7æ èµ·ï¼æ¤ç¹æ§å·²å¨ä¸»æµæµè§å¨ä¸å¾å°æ¯æï¼å¯å¨å¤§å¤æ°è®¾å¤åæµè§å¨çæ¬ä¸æ£å¸¸ä½¿ç¨ã
createCDATASection() å建并è¿åä¸ä¸ªæ°ç CDATA çæ®µèç¹ã
è¯æ³
var CDATASectionNode = document.createCDATASection(data);
- CDATASectionNode æ¯ä¸ä¸ª CDATA çæ®µèç¹ã
- data å å«äºè¦è¢«æ·»å è³ CDATA çæ®µçæ°æ®çå符串ã
示ä¾
js
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_ERRexception 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 > # ref-for-dom-document-createcommentâ > |