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

设置背景颜色的时候为什么要用this?

        var oTr = document.getElementsByTagName("tr");
        for(var i=0;i<oTr.length;i++){
            oTr[i].onmouseover = function(){
                this.style.backgroundColor="#f2f2f2";
            }
            oTr[i].onmouseout = function() {
                this.style.backgroundColor="#fff";
            }
        }

在上面这代码中,鼠标经过而触发改动背景颜色的函数中,为什么要用“this.style.backgroundColor="#fff";”  我一开始是设想使用“oTr[i].style.backgroundColor="#fff";",结果失败了。

求帮忙告知下失败的原因是什么,谢谢!!!

正在回答

1 回答

http://img1.sycdn.imooc.com//56dac83d0001eb6410694600.jpg

找到一篇博客讲这个问题,貌似是函数中必须用this来调用属性和方法,我也试了用trs[i],达不到效果

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

举报

0/150
提交
取消

设置背景颜色的时候为什么要用this?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信