最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access the outer this from jQuery functions? - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question asked Apr 11, 2011 at 1:05 PaoloPaolo 21.2k21 gold badges76 silver badges122 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10
var that = this;
function (idx, el) {
    // access what used to be this.color as that.color
}
发布评论

评论列表(0)

  1. 暂无评论