调度名称中带有点的 CustomEvent 不会触发 jQuery.on() 事件侦听器

发布于 2025-01-13 08:07:54 字数 602 浏览 5 评论 0原文

我使用了 jQuery 版本 2.2.4 并尝试捕获事件 - 没有运气。有什么办法可以解决问题吗?

此代码有效:

window.addEventListener('test.namespace', e => console.log('CustomEvent with namespace captured by vanilla'));

这不起作用:

$(window).on('test.namespace', e => console.log('CustomEvent with namespace captured by jQuery'));

const event = new CustomEvent('test.namespace', {
  bubbles: true,
  detail: 123
});
window.dispatchEvent(event);

http://jsbin.com/tecupavuji/edit? html、js、控制台

I've used jQuery version 2.2.4 and tried to capture event - no luck. Is there any way to fix issue?

This code works:

window.addEventListener('test.namespace', e => console.log('CustomEvent with namespace captured by vanilla'));

This does not work:

$(window).on('test.namespace', e => console.log('CustomEvent with namespace captured by jQuery'));

const event = new CustomEvent('test.namespace', {
  bubbles: true,
  detail: 123
});
window.dispatchEvent(event);

http://jsbin.com/tecupavuji/edit?html,js,console

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

爱格式化 2025-01-20 08:07:54

“普通”DOM 事件系统没有命名空间的概念。 window.addEventListener('test.namespace', ...) 实际上将监听名为 test.namespace 的事件,这是您使用 <代码>new CustomEvent('test.namespace', ...).

事件命名空间是 jQuery 中的一个概念:

事件名称可以通过事件命名空间来限定,从而简化删除或触发事件的过程。例如,"click.myPlugin.simple" 定义了此特定点击事件的 myPlugin 和 simple 命名空间。通过该字符串附加的点击事件处理程序可以使用 .off("click.myPlugin").off("click.simple") 删除,而不会干扰其他点击处理程序附加到元素。

这里重要的是命名空间不是事件名称的一部分。这就是为什么 new CustomEvent('test.namespace', ...) 不起作用,但 new CustomEvent('test', ...) 可以。

如果你想触发特定命名空间的事件,你必须使用 jQuery:

$(window).trigger('test.namespace', {detail: 123});

$(window).on('test.namespace', e => console.log('CustomEvent with namespace captured by jQuery'));

$(window).trigger('test.namespace', {detail: 123});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

The "normal" DOM event system doesn't have a concept of namespaces. window.addEventListener('test.namespace', ...) will literally be listening for an event with the name test.namespace, which is what you are creating with new CustomEvent('test.namespace', ...).

Event namespaces is a concept in jQuery:

An event name can be qualified by event namespaces that simplify removing or triggering the event. For example, "click.myPlugin.simple" defines both the myPlugin and simple namespaces for this particular click event. A click event handler attached via that string could be removed with .off("click.myPlugin") or .off("click.simple") without disturbing other click handlers attached to the elements.

What's important here is that the namespace is not part of the event name. That's why new CustomEvent('test.namespace', ...) doesn't work, but new CustomEvent('test', ...) would.

If you want to trigger an event for a specific namespace, you have to use jQuery for that:

$(window).trigger('test.namespace', {detail: 123});

$(window).on('test.namespace', e => console.log('CustomEvent with namespace captured by jQuery'));

$(window).trigger('test.namespace', {detail: 123});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

说不完的你爱 2025-01-20 08:07:54

前面的答案并没有真正解决如何为来自第三方(非 jQuery)库并且事件名称中有一个点的自定义事件注册处理程序的问题。假设该事件是 foo.bar

简而言之,jQuery 没有办法为此类事件安装处理程序。

$(window).on( 'foo.bar' ) 将为事件 foo 安装一个处理程序。

似乎没有任何方法可以转义名称中的点,或者强制 jQuery 为名为 foo.bar 的事件安装处理程序。

The previous answer doesn't really address the problem of how to register a handler for a custom event which comes from a third-party (non-jQuery) library and which has a dot in the event name. Say the event is foo.bar.

The short answer is that jQuery doesn't have a way to install a handler for such an event.

$(window).on( 'foo.bar' ) will instead install a handler for the event foo.

There doesn't seem to be any way to escape the dot in the name or otherwise coerce jQuery into installing a handler for an event named foo.bar.

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