JavaScript循环问题,请求大神们帮忙啊!!
目的:遍历颜色添加到所有a标签中,代码如下:varcolors=['#FF8671','#EF404B','#12F5FA','#00BFFF','#FDE377','#...
目的:遍历颜色添加到所有a标签中,代码如下:
var colors = ['#FF8671','#EF404B','#12F5FA','#00BFFF','#FDE377','#7AFF4D','#DFBFFF','#FF7F00','#FF00FF','#00FF7F'];
var aArr = document.getElementsByTagName("a");
//循环
var index = 0;
for(var i = 0; i < colors.length; i++){
aArr[index].style.color = colors[i];
if(i = colors.length-1 && index < aArr.length){
i = 0;
}else {
break;
}
index++;
}
可是总是报错 Uncaught TypeError: Cannot read property 'style' of undefined(…),而且所有颜色都是第一个,到底哪里不对啊啊啊 展开
var colors = ['#FF8671','#EF404B','#12F5FA','#00BFFF','#FDE377','#7AFF4D','#DFBFFF','#FF7F00','#FF00FF','#00FF7F'];
var aArr = document.getElementsByTagName("a");
//循环
var index = 0;
for(var i = 0; i < colors.length; i++){
aArr[index].style.color = colors[i];
if(i = colors.length-1 && index < aArr.length){
i = 0;
}else {
break;
}
index++;
}
可是总是报错 Uncaught TypeError: Cannot read property 'style' of undefined(…),而且所有颜色都是第一个,到底哪里不对啊啊啊 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询