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

来源:9-22 编程练习

偏爱花开的声音

2015-06-21 15:39

为什么下面这段代码中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";
      } 
}
}


写回答 关注

1回答

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

    trs[i].onmouseover=function(){

              this.style.backgroundColor="#f2f2f2";         

          }

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

    偏爱花开的声...

    谢谢~~ alert(trs[i]);是undefined alert(this);是[object HTMLTableRowElement] 但是不理解为什么trs[i]是undefined,觉得应该也是[object HTMLTableRowElement]才对啊~

    2015-06-22 12:21:31

    共 1 条回复 >

JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

468746 学习 · 22132 问题

查看课程

相似问题