1
我是新来的JS,并试图学习一些基本的东西。我已经花了几个小时,但我认为它不应该那么困难。出于某种原因,我的电脑没有意识到我在询问childNodes。为什么childNodes不能在这个脚本中工作?
这是一个简单的脚本,只是试图计算我有的锂标签的数量。我知道还有其他方法可以做到这一点,但我正在尝试学习这种方式。
<title>To-Do List</title>
<script>
function findComments(){
var bodyTag = document.getElementsByTagName("ol");
var count = 0;
for(var i=0;i<bodyTag.childNodes.length;i++){
if(bodyTag.childNodes[i].nodeType == 1){
count++;
}
}
alert(count);
}
window.onload = findComments;
</script>
<!--List is declared-->
<ol id="toDoList">
<!--List Items are created-->
<li>Mow the lawn</li>
<li>Clean the windows</li>
<li>Answer your email</li>
</ol>
<!--Main paragraph-->
<p id="toDoNotes">Make sure all these are completed by 8pm so you can watch the game on TV!</p>
<script>
</script>
你的变量指向'ol'被称为'bodyTag'? –