javascript – 使用jQuery.bind保留’this’?

前端之家收集整理的这篇文章主要介绍了javascript – 使用jQuery.bind保留’this’?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将一个事件绑定到一个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));

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

原文链接:https://www.f2er.com/jquery/643724.html

猜你在找的jQuery相关文章