我想将一个事件绑定到一个jQuery元素,因此它调用了我的类中的函数,但是当调用该函数时,该变量不再指向该类,而是指向sender元素.
this.remove = function() { alert(this); /* Shows [object HTMLImageElement] instead of the desired class */ this.dv.remove(); /* error */ } $(this.buttons['cancel']).bind("click",this.remove);
我怎么能绕过那个?
解决方法
$(this.buttons [‘cancel’]).bind(“click”,$.proxy(this.remove,this));
其中第二个参数是您希望方法执行的上下文.