如何将 $(this) 对象传递给单击事件内的函数。
$('#mainwrap img').click(function(){
mcImageManager.browse({
oninsert : function(o) {
src= o.focusedFile.url;
$(this).attr("src", src );
}
});
});
如有任何建议,我们将不胜感激。
最佳答案
this
是一个特殊变量 - 每次输入函数时它都会采用一个新值。
在这种情况下,要使外部函数的 this
在内部函数中可用,您可以在嵌套函数外部将一个新变量绑定(bind)到它:
$('#mainwrap img').click(function() {
var $this = $(this); // new variable here
mcImageManager.browse({
oninsert : function(o) {
src= o.focusedFile.url;
$this.attr("src", src ); // referenced here
}
});
});
关于jquery - 从嵌套函数访问当前对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15543219/