您将看到只有2个td对象丢失了clasNname。为什么变量"很多"在每个className =""?中丢失一个itens谁能帮我?谢谢。
<!DOCTYPE html>
<html>
<head>
<title>Why?</title>
<style>
.f{background:lightblue;}
.g{background:lightcoral;}
</style>
</head>
<body>
<table id="t" style="border:1px solid black">
<tr>
<td class="f">1</td>
<td class="f">2</td>
<td class="f">3</td>
<td class="f">4</td>
</tr>
</table>
<script>
many = document.getElementById("t").getElementsByClassName("f");
document.write("initial length of many="+many.length+"<br>");
for (var x = 0; x < many.length; x++) {
many[x].className='';
document.write("loop x="+x+" many.length="+many.length+"<br>");
}
document.write("final length of many="+many.length+"<br>");
</script>
</body>
</html>
&#13;
答案 0 :(得分:0)
getElememtsByClassName()
方法返回一个实时NodeList,因此如果删除那些不再具有该类名的元素搜索的类名,则立即从该NodeList中删除在many
变量中。