原代码:
for(var i=0; i<li_obj.length; i++) {
li_obj[i].onmouseover = function() {
li_obj[i].style.backgroundColor = "blue";
}
}
报错:Uncaught TypeError: Cannot read property 'style' of undefined(…)
解决方法:
for(var i=0; i<li_obj.length; i++) {
li_obj[i].index = i;
li_obj[i].onmouseover = function() {
li_obj[this.index].style.backgroundColor = "blue";
}
}
或
for(var i=0; i<li_obj.length; i++) {
li_obj[i].onmouseover = function() {
this.style.backgroundColor = "blue";
}
}