js 中object.childNodes.length undefined是什么原因呢?

html代码:

<!DOCTYPE html>
<html>
<head>
    <title>test4.4</title>
    
    <script type="text/javascript" src="D:\draft\primaryJavascript\test44.js"></script>
</head>
<body>
<p>why childNodes are being undefined?</p>
<h1>Quagmire</h1>
<ul>
    <li>
        <a href="D:\draft\primaryJavascript\fireworks.jpg" onclick="showPig(this);return false;" title="a fireworks display">BoomShakaLaka</a>
    </li>
    <li>
        <a href="D:\draft\primaryJavascript\coffee.jpg" onclick="showPig(this);return false;" title="a cup of black coffee">WannaDie?</a>
    </li>
    <li>
        <a href="D:\draft\primaryJavascript\BigBen.jpg" onclick="showPig(this);return false;" title="the famous clock">the giant clock is going to hit you directly.</a>
    </li>
    <img id="placeholder" src="D:\draft\primaryJavascript\placeholder.jpg"/ alt="my image gallery"/>
</ul>
</body>
</html>

js代码:

function countBodyChildren(){
    var body_element=document.getElementsByTagName("body")[0];
    alert(body_element.chilNodes.length);
}
window.onload=countBodyChildren;

console中报错:

test44.js:9 Uncaught TypeError: Cannot read property 'length' of undefined
    at countBodyChildren (test44.js:9)
countBodyChildren @ test44.js:9
阅读 2.8k
1 个回答

书写错误吧,是childNodes不是chilNodes

function countBodyChildren(){
    var body_element=document.getElementsByTagName("body")[0];
    alert(body_element.childNodes.length);
}
window.onload=countBodyChildren;
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题