gpt4 book ai didi

javascript - 使用 jQuery.bind 保留 'this'?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:57:44 26 4
gpt4 key购买 nike

我想将事件绑定(bind)到 jQuery 元素,以便它调用我类中的函数,但是当函数被调用时,this 变量不再指向类,而不是指向发送者元素。

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("点击", $.proxy(this.remove, this));

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

关于javascript - 使用 jQuery.bind 保留 'this'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7164540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com