關于setAttribute的問題
<div?class="food"?id="herd">
???
</div>
<h1?onclick="hattr()">trjtr</h1>
<script>
??function?hattr(){
??????var?anode=document.getElementsByClassName("food");
???????anode[0].setAttribute("id","987");
??????anode[0].setAttribute("class","789");
??}為什么?? anode[0]??? 就沒有問題;而?? anode?? 就不正確了
2015-07-15
getElementsByClassName一般得到的是一個對象數組,如果根據id查找返回的就是對象
2015-07-15
我找到問題了,是我大意了。 所有 getElementsByClassName() 方法返回的是元素的數組,而不是一個元素