使用jQuery.bind保留'this'?

Kri*_*oks 3 javascript jquery class

我想将一个事件绑定到一个jQuery元素,因此它调用了我的类中的函数,但是当调用该函数时,this变量不再指向该类,而是指向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);
Run Code Online (Sandbox Code Playgroud)

我怎么能绕过那个?

J. *_*mes 8

$(this.buttons['cancel']).bind("click", $.proxy(this.remove, this));

其中第二个参数是您希望方法执行的上下文.