jQuery/Javascript 暂时禁用 addEventListener/attachEvent 附加的事件

发布于 2024-10-19 14:14:26 字数 835 浏览 2 评论 0原文

有没有办法暂时禁用事件侦听器?

就我而言,我有一个第三方库(以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

錯遇了你 2024-10-26 14:14:26

jQuery 使用 data 在内部存储事件,因此您可以使用它获取对象的所有事件处理程序:

$("#foo").data("events")

然后,您可以使用 unbind

$("#foo").unbind('click', $("#foo").data("events").click[42]);

jQuery uses data to store events internally, so you can use it to get all of the event handlers for an object:

$("#foo").data("events")

You can then remove a specific handler by using unbind:

$("#foo").unbind('click', $("#foo").data("events").click[42]);
下雨或天晴 2024-10-26 14:14:26

不幸的是,您无法访问它们。最多,您可以使用 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 Microsofts detachEvent (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.

浮萍、无处依 2024-10-26 14:14:26

如果您想暂时禁用正在运行的事件处理程序,为什么不直接向函数添加转义码呢?

像这样:

$('#button').click(function(){
    var clicked_element = $(this);
    if(elem.hasClass('event-click-disabled'))
    {
        // logging code so we know exactly what events are being skipped
        console.info(
            'The click event on following element was skipped',
            clicked_element
        );
        return;
    }
    alert('Button clicked');
});

然后,如果您想禁用特定元素上的事件,只需调用

element.addClass('event-click-disabled');

事件处理程序仍然运行,但它会立即返回。

If you want to temporarily disable an event handler being run, why not just add escape code to the function?

like so:

$('#button').click(function(){
    var clicked_element = $(this);
    if(elem.hasClass('event-click-disabled'))
    {
        // logging code so we know exactly what events are being skipped
        console.info(
            'The click event on following element was skipped',
            clicked_element
        );
        return;
    }
    alert('Button clicked');
});

Then if you want to disable an event on a specific element, just call

element.addClass('event-click-disabled');

The event handler is still run, but it will return immediately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文