我正在尝试计算身体中的元素数量,而我正在这样做
<html>
<head>
<title>Practice Javascript</title>
<script>
window.onload = count;
function count()
{
var ele = document.body.childNodes;
alert(ele.length);
}
</script>
</head>
<body>
<p>Text one</p>
<p>Text Two</p>
</body>
</html>
我期待输出为 2 但它是 5
但是当我通过以下代码删除新行时
<html>
<head>
<title>Practice Javascript</title>
<script>
window.onload = count;
function count()
{
var ele = document.body.childNodes;
alert(ele.length);
}
</script>
</head>
<body><p>Text one</p><p>Text Two</p>
</body>
</html>
它给出输出 3 Dom 树如何评估不存在的子节点?