我在按钮上有一个单击事件,我必须传递此类对象来调用单击事件内的函数。我的要求如下
$(this).on('click', function(){
this.callFunc();
});
目前,我将其存储在 self 中并在点击函数中使用它。目前我有以下代码来实现此目的。
var self = this;
$(this).on('click', function(){
self.callFunc();
});
有没有办法直接使用这个内部点击功能?
最佳答案
使用$.proxy将自定义执行上下文传递给回调函数
$(this).on('click', $.proxy(function(){
this.callFunc();
}, this));
关于javascript - 在点击事件函数中使用 this(类对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16477703/