开发者

How to access the outer this from jQuery functions?

out of curiosity is there a way to access this.color from the paint开发者_运维技巧 function?

function Foo(color)
{
    this.color = color;
    this.paint = function paint()
    {
       $("select").each(function(idx, el)
        {
            $(el).css("background", color); // OK
            // $(el).css("background", this.color); // this.color is undefined
        })
    }
}

new Foo("red").paint();

Thanks


var that = this;
function (idx, el) {
    // access what used to be this.color as that.color
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜