日期:2014-05-16  浏览次数:20293 次

关于js中childNodes值的问题
在下面的页面中输出的是8 first,8 two,9 three,为什么会不同呢?
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>js_demo1</title>
</head>
<body>

<h1>列表</h1>
<p title="nice man">o(∩_∩)o 哈哈</p>
<ul id="ulid">
<li title="one wow">one</li>
<li title="two wow">two</li>
<li title="three wow">three</li>
</ul>
<script type="text/javascript">
function al(){
alert(document.body.childNodes.length+" three");
}
window.onload = al;
alert(document.body.childNodes.length+" first");
alert(document.getElementsByTagName("body")[0].childNodes.value);
</script>
</body>
</html>

------解决方案--------------------
var as = [];
for(var i=0;i<document.body.childNodes.length;i++){
   as.push(document.body.childNodes[i].tagName);
}
alert(as.join(','));

你输出下就知道了