鍍金池/ 問答/HTML/ js 中object.childNodes.length undefined是什

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
回答
編輯回答
法克魷

書寫錯誤吧,是childNodes不是chilNodes

function countBodyChildren(){
    var body_element=document.getElementsByTagName("body")[0];
    alert(body_element.childNodes.length);
}
window.onload=countBodyChildren;
2017年11月7日 06:55