<body>
<p>123</p>
111
</body>
js:
<script>
window.onload = function () {
console.log("body的childNodes");
var oItems = document.body.childNodes;
console.log(oItems.length);
for (var i = 0; i < oItems.length; i++) {
console.log("nodeType:" + oItems[i].nodeType);
console.log("nodeName:" + oItems[i].nodeName);
console.log("nodeValue:'" + oItems[i].nodeValue + "'");
}
};
</script>
console.log输出值:
可以看到childnode有3个值:p前面文本节点,p元素节点,p后面文本节点。
body的childNodes
3
nodeType:3
nodeName:#text
nodeValue:'
'
nodeType:1
nodeName:P
nodeValue:'null'
nodeType:3
nodeName:#text
nodeValue:'
111
'
如果想要p的innerText,直接
oItems[1].innerText
网友评论