以下代碼,將this改成rows[i]爲啥不起做用了數組
var rows = document.getElementsByTagName("tr"); for(var i=0;i<rows.length;i++){ rows[i].onmouseover=function(){ this.style.backgroundColor="#f2f2f2"; }; rows[i].onmouseout=function(){ this.style.backgroundColor="#fff"; }; }
function () {
rows[i].style.backgroundColor = '#f2f2f2';
};
rows[i].onmouseover = (function (n) {
return function () {
rows[n].style.backgroundColor = '#f2f2f2';
}
}(i));
for (var i = 0; i < rows.length; i++) {
rows[i].index = i;
rows[i].onmouseover = function () {
rows[this.index].style.backgroundColor = '#f2f2f2';
}
}
// 經過給對應項添加屬性 index 來保存 i 的值