示例
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
myFunction(xhttp);
}
};
xhttp.open("GET", "books_cdata.xml", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName("html")[0].childNodes[0];
document.getElementById("demo").innerHTML =
x.data + " - Length: " + x.length;
}
</script>
</body>
</html>
尝试一下
请记住,上面尝试一下的运行结果长度来自整个CDATA部分(<b>Stunning!</b>),而不仅仅是文本。