0
我认为“document.documentElement.cildNodes”是标签所有的childNodes和以前一样,但今天我做我的代码excrise,我发现一个特殊情况:javascript为什么“document.documentElement.childNodes”输出不同的结果?
<!DOCTYPE html>
<html>
<head>
<title>javascript</title>
<script>
var o = document.createElement('script');
o.text = 'alert("111")';
var ohtml = document.documentElement;
alert(ohtml.nodeName); //output HTML
alert(ohtml.childNodes.length); //nodes length is 1
alert(ohtml.childNodes.length); //just head
ohtml.childNodes[0].appendChild(o);
function shownode() {
var ohtml = document.documentElement;
alert(ohtml.nodeName);
alert(ohtml.childNodes.length); //nodes length is 3
alert(ohtml.childNodes[0].nodeName); //head
alert(ohtml.childNodes[1].nodeName); //#text
alert(ohtml.childNodes[2].nodeName); //body
}
</script>
</head>
<body><div>test</div><input id="Button1" type="button" value="show nodes" onclick="shownode();" />
</body>
</html>
为什么我EXCUTE“document.documentElement.childNodes”在标签中的标签和功能会得到不同的结果? 我希望有人能给我更多关于这个的例子。非常感谢!