<ul class="ul"><li>red</li>
<li>1</li>
<li>1</li>
<li>1</li>
</ul>
<ul class="ul ul-2"><li>red</li>
<li>1</li>
<li>1</li>
<li>1</li>
</ul>
<script type="text/javascript">
var ulArry = document.getElementsByClassName("ul");
for (var i in ulArry) {
ulArry[i].firstChild.style.color = "#FF0000";
}
</script>
li的颜色已经改变了,但是为什么控制台还报错?
typecho2017-07-05 11:00:11
ulArry是NodeList对象,for in获取到的不只是两个Dom节点,还有length,item(),namedItem() 。所以运行到length时就报错了。
你可以在循环中打印出 ulArry[i]看看