新手提问一个关于javascript DOM解析的问题
<html> 节点的字节点不包括 <body> 吗?还是在firefox下将body不认为是一个节点?代码如下
<html>
<head>
<title> DOM Example </title>
<script language= "javascript ">
var oHtml = document.documentElement;
var oHead = oHtml.firstChild;
var oBody = oHtml.lastChild;
alert(oHead.nodeName);
alert(oBody.nodeName);
alert(oHtml.childNodes.length);
</script>
</head>
<body>
</body>
</html>
上述代码在firefox下运行结果为head head 1 为啥啊?
------解决方案--------------------代码执行的时候body还未加载,
改成下面的就对了
<html>
<head>
<title> DOM Example </title>
</head>
<body>
</body>
<script language= "javascript ">
var oHtml = document.documentElement;
var oHead = oHtml.firstChild;
var oBody = oHtml.lastChild;
alert(oHead.nodeName);
alert(oBody.nodeName);
alert(oHtml.childNodes.length);
</script>
</html>