为什么"Script"是"Body"的孩子?



首先,我不擅长HTML/CSS/Javascript,我只是想通过考试。我有以下代码:

<script>
window.onload=function() {
var c = document.body.childNodes;
var txt = "";
var i;
for (i = 0; i < c.length; i++) {
txt = txt + c[i].nodeName + "<br>";
}
document.getElementById("demo").innerHTML += txt;
alert(document.getElementById("demo").childNodes[0].nodeName);
}
</script>
</head>
<body><!-- Comm -->
<p>PPPP</p>
<div>DIVV</div>
<p>IMGGG</p><br>
<img src="someimg.jpg" alt="!!!">
<p id="demo"><strong>document.body.childNodes:</strong><br>
</p>
</body>

我的html页面现在看起来是这样的:

PPPP
DIVV
IMGGG

!!!
document.body.childNodes:
#comment
#text
P
#text
DIV
#text
P
BR
#text
IMG
#text
P
#text
#comment
#text
SCRIPT
#text

最后两行("SCRIPT"one_answers"#text"(从哪里来?

请查看源和输出的完整列表。在您的示例中,最后一个5标记似乎有问题。

来源

<!doctype html>
<html>
<head>
<script>
"use strict";
window.addEventListener('load', onLoaded, false);
function onLoaded(evt)
{
var i, txt='', c = Array.from(document.body.childNodes), tgt=document.getElementById("demo");
for (i = 0; i < c.length; i++) 
{
txt = c[i].nodeName + "<br>";
tgt.innerHTML += txt;
}
alert(document.getElementById("demo").childNodes[0].nodeName);
}
</script>
</head>
<body><!-- Comm -->
<p>PPPP</p>
<div>DIVV</div>
<p>IMGGG</p><br>
<img src="someimg.jpg" alt="!!!">
<p id="demo"><strong>document.body.childNodes:</strong><br>
</p></body>
</html>

输出

PPPP
DIVV
IMGGG

!!!
document.body.childNodes:
#comment
#text
P
#text
DIV
#text
P
BR
#text
IMG
#text
P
#text

详细信息

  • IMG-是图像
  • #text-是IMG和P之间的空白
  • P-是<p id='demo'>
  • #text-是浏览器在</p></body>之间插入的空白

最新更新