为了账号安全,请及时绑定邮箱和手机立即绑定

关于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";
      } 
}
}


正在回答

1 回答

trs[i].onmouseover=function(){

          this.style.backgroundColor="#f2f2f2";         

      }

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

0 回复 有任何疑惑可以回复我~
#1

偏爱花开的声音 提问者

谢谢~~ alert(trs[i]);是undefined alert(this);是[object HTMLTableRowElement] 但是不理解为什么trs[i]是undefined,觉得应该也是[object HTMLTableRowElement]才对啊~
2015-06-22 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
JavaScript进阶篇
  • 参与学习       469016    人
  • 解答问题       22582    个

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

进入课程

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

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号