如何从jQuery函数访问外部this



出于好奇,是否有办法从paint函数访问this.color ?

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();

谢谢

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

最新更新