CDATA 数据,其不被浏览器解析显示出来,注意 CDATA数据的写法
<body>
<div id="myDiv"><![CDATA[This is some content.]]></div>
<input type="button" value="Get CData Section" onclick="getCDataSection()" />
<p>Note that this example uses XHTML, since CData sections are not supported in HTML.</p>
<script type="text/javascript">
function getCDataSection(){
var div = document.getElementById("myDiv");
var cdata = div.firstChild;
console.log(cdata.nodeType);
console.log(cdata.nodeValue);
}
</script>
</body>
获取注释内容
<body>
<div id="myDiv"><!-- A comment --></div>
<input type="button" value="Get Comment" onclick="getComment()">
<script type="text/javascript">
function getComment(){
var div = document.getElementById("myDiv");
var comment = div.firstChild;
alert(comment.data); // A comment
}
</script>
</body>