简体   繁体   中英

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM