Body 元素上的子节点数为 1,即使那里什么也没有。这只发生在 Body 元素上,而不是其他元素,例如 div。在这种情况下,结果为 0。这是为什么呢?
<head>
<script>
function run() {
alert(document.body.childNodes.length);
}
window.addEventListener("load", run, false);
</script>
</head>
<body></body>
</html>
而结果是 0
<!Doctype html>
<html>
<head>
<script>
function run() {
alert(document.body.getElementsByTagName("div")[0].childNodes.length);
}
window.addEventListener("load",run,false);
</script>
</head>
<body>
<div></div>
</body>
</html>