删除绑定到处理程序的jQuery事件(Remove jQuery event where this is bound to the handler)

编程入门 行业动态 更新时间:2024-10-28 10:35:18
删除绑定到处理程序的jQuery事件(Remove jQuery event where this is bound to the handler)

我试图删除一个jQuery事件谁回调绑定了this 。 问题是.bind()每次创建一个新函数...所以当我尝试删除事件时,它在技术上不是相同的功能,所以它不起作用。

在这种情况下,我无法弄清楚如何在回调中保持正确:

class Controller { open() { $('body').on('click', this.handleClick.bind(this)); } close() { $('body').off('click', this.handleClick.bind(this)); } }

I am trying to remove a jQuery event who's callback is bound with this. The problem is that .bind() creates a new function every time... so when I try removing the event, it is technically not the same function so it doesn't work.

I can't figure out how to keep the right this inside the callback in this case:

class Controller { open() { $('body').on('click', this.handleClick.bind(this)); } close() { $('body').off('click', this.handleClick.bind(this)); } }

最满意答案

您可以简单地使用jQuery名称空间:

class Controller { open() { $('body').on('click.myNamespace', this.handleClick.bind(this)); } close() { $('body').off('click.myNamespace'); } }

You can simply use jQuery namespaces:

class Controller { open() { $('body').on('click.myNamespace', this.handleClick.bind(this)); } close() { $('body').off('click.myNamespace'); } }

更多推荐

本文发布于:2023-07-20 07:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1195946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   事件   程序   jQuery   handler

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!