jQuery/Javascript 暂时禁用 addEventListener/attachEvent 附加的事件
有没有办法暂时禁用事件侦听器?
就我而言,我有一个第三方库(不以 jQuery 为中心),它使用 addEventListener/attachEvent 在元素上创建 mouseover/mouseout 事件。
在某些情况下,另一个事件会在不同的元素上触发,我需要禁用这些事件侦听器。到目前为止,我的解决方案是简单地取消绑定
鼠标悬停/鼠标悬停。这个通常工作得很好,因为该事件通常会导致页面刷新。
但是,时不时地可能会发生错误(认为验证错误),导致页面不刷新,并且我需要重新附加 mouseover/mouseout 事件侦听器。
有用的信息
可能值得一提的是,因为 mouseover/mouseout 事件侦听器是在第三方库中创建并附加的,所以我不能简单地将事件分配给变量并以这种方式绑定/取消绑定它(AFIK是执行此操作的最佳方法)。
更新
我最初问过
jQuery 中有没有办法获取已分配给对象的事件侦听器?
我发现不可能访问由 addEventListener/attachEvent 分配的事件: 访问JavaScript中通过attachEvent() / addEventListener()添加的事件
Is there a way to temporarily disable an event listener?
In my case, I have a third party library (not jQuery centric) that creates mouseover/mouseout events on an element using addEventListener/attachEvent.
Under certain circumstances another event fires on a different element and I need to disable those event listeners. My solution thus far has been to simply unbind
the mouseover/mouseout. This usually works fine because that event generally causes the page to refresh.
However, every now and again an error can occur (think validation error) that results in the page not refreshing, and I need to re-attach the mouseover/mouseout event listeners.
Helpful information
It's probably worth mentioning that because the mouseover/mouseout event listeners are created and attached within a third party library I cannot simply assign the event to a variable and bind/unbind it in that manner (which AFIK is the best way to do this).
Update
I had originally asked
Is there a way in jQuery to get the event listeners already assigned to an object?
I have since found out it is impossible to access events assigned by addEventListener/attachEvent: Access events added with attachEvent() / addEventListener() in JavaScript
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jQuery 使用
data
在内部存储事件,因此您可以使用它获取对象的所有事件处理程序:然后,您可以使用
unbind
:jQuery uses
data
to store events internally, so you can use it to get all of the event handlers for an object:You can then remove a specific handler by using
unbind
:不幸的是,您无法访问它们。最多,您可以使用 W3C 的
removeEventListener
删除事件侦听器(docs< /a>) 和/或 Microsoft 的detachEvent
(文档)。然而,一旦监听器被移除,它就永远消失了。removeEventListener
有一个警告,即如果事件注册了两次,一次指示捕获,一次指示不捕获,则必须将其删除两次;每个案例一次。要了解有关捕获和不捕获的更多信息,请参阅 W3C 规范。
Unfortunately, you can't access them. At best, you can remove event listeners using W3C's
removeEventListener
(docs) and/or MicrosoftsdetachEvent
(docs). Once the listener is removed, however, it's gone for good.There's one caveat with
removeEventListener
, in that if the event was registered twice, once indicating to capture, and once indicating not to capture, you must remove it twice; once for each case.To learn more about capturing and not capturing, see the W3C spec.
如果您想暂时禁用正在运行的事件处理程序,为什么不直接向函数添加转义码呢?
像这样:
然后,如果您想禁用特定元素上的事件,只需调用
事件处理程序仍然运行,但它会立即返回。
If you want to temporarily disable an event handler being run, why not just add escape code to the function?
like so:
Then if you want to disable an event on a specific element, just call
The event handler is still run, but it will return immediately.