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
}
精彩评论