问答详情
源自:9-22 编程练习

关于this的疑问?菜鸟求解答~~

为什么下面这段代码中onmouseover()函数中这一句:

this.style.backgroundColor="#f2f2f2";

写成:

trs[i].style.backgroundColor="#f2f2f2";

结果就不对了呢?!谢谢大家~~

window.onload = function(){
                  
     // 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。
     var trs = document.getElementsByTagName("tr");
for(var i = 0;i<trs.length;i++){
      trs[i].onmouseover=function(){
          this.style.backgroundColor="#f2f2f2";
        
      }
      trs[i].onmouseout=function(){
          this.style.backgroundColor="#fff";
      } 
}
}


提问者:偏爱花开的声音 2015-06-21 15:39

个回答

  • chavin
    2015-06-22 00:19:43

    trs[i].onmouseover=function(){

              this.style.backgroundColor="#f2f2f2";         

          }

    你在这里面分别alert(trs[i]);和alert(this);比较一下