function highlight(){ var tbody=document.getElementById("table").lastChild; var trs=tbody.getElementsByTagName("tr"); for(i=1;i<trs.length;i++){ trs[i].onmouseover=function(){ this.style.backgroundColor="#f2f2f2"; } trs[i].onmouseout=function(){ this.style.backgroundColor="#ffffff"; } } }
1 回答
nickylau82
TA贡献128条经验 获得超67个赞
因为js也是面向对象的,当你结束的时候i已经固定等于5了。所以你用trs[i]就相当于是trs[5]了
另外你的代码有三个问题:在循环的时候声明i的时候需要加上var,还有i的初始值应该为0,另外就是每个赋值表达式后面最好加上分号。
function highlight(){
var tbody=document.getElementById("table").lastChild;
var trs=tbody.getElementsByTagName("tr");
for(var i=0;i<trs.length;i++){
(function(index){
trs[index].onmouseover=function(){
trs[index].style.backgroundColor="#f2f2f2";
};
trs[index].onmouseout=function(){
trs[index].style.backgroundColor="#ffffff";
};
})(i);
}
}
highlight();添加回答
举报
0/150
提交
取消
