开发者

How to access the outer this from jQuery functions?

开发者 https://www.devze.com 2023-02-23 19:28 出处:网络
out of curiosity is there a way to access this.color from the paint开发者_运维技巧 function? function Foo(color)

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

精彩评论

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