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

设置背景颜色的时候为什么要用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";",结果失败了。

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

提问者:Elvis_Nam 2016-03-05 14:53

个回答

  • 长颈鹿在附近
    2016-03-05 19:53:22

    http://img.mukewang.com/56dac83d0001eb6410694600.jpg

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