javascript jQuery 中的全局自定义事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3279809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
global custom events in jQuery
提问by AnC
I want to use custom jQuery events independent of DOM elements, but I'm not sure what the best way is to achieve this.
我想使用独立于 DOM 元素的自定义 jQuery 事件,但我不确定实现这一点的最佳方法是什么。
Here's what I started out with:
这是我开始的内容:
// some system component registers an event handler
$().bind("foo.bar", handler); // foo is my app's namespace, bar is event name
// another part of the system fires off the event
$().trigger("foo.bar", { context: "lorem ipsum" });
After looking at jQuery's source, in particular its handling of global AJAX events, I figured this should work:
在查看了 jQuery 的源代码,特别是它对全局 AJAX 事件的处理后,我认为这应该有效:
$.fn.bind("foo.bar", handler);
// ...
$.event.trigger("foo.bar", { context: "lorem ipsum" });
However, it appears that my handler function is never even called.
但是,似乎我的处理程序函数从未被调用过。
Am I perhaps going about this the wrong way?
我可能会以错误的方式解决这个问题吗?
回答by James
If you're using jQuery >1.4then $()returns an empty jQuery collection which would mean that no event handler is actually bound to anything. Before 1.4it would have returned the same as jQuery(document).
如果您使用 jQuery,>1.4则$()返回一个空的 jQuery 集合,这意味着实际上没有事件处理程序绑定到任何内容。之前1.4它会返回与jQuery(document).
It might be better to simply have a global namespace (an actual object) and then add events to that:
简单地拥有一个全局命名空间(一个实际对象)然后向其中添加事件可能会更好:
var FOO = {};
$(FOO).bind("foo.bar", handler);
$(FOO).trigger("foo.bar", { context: "lorem ipsum" });
回答by stephenrs
I found my way here because I was looking to implement the publisher/subscriber pattern using namespaced custom events using jQuery. While the accepted solution is a way to use $.event.trigger() in a way that is not tied to DOM elements, it won't work well for a true global event implementation in a publisher/subscriber architecture (such as with a complex UI with many asynchronous actions), where you want to have arbitrary objects/elements listen for a custom event.
我在这里找到了自己的方法,因为我希望使用 jQuery 使用命名空间自定义事件来实现发布者/订阅者模式。虽然公认的解决方案是一种以不依赖于 DOM 元素的方式使用 $.event.trigger() 的方法,但它不适用于发布者/订阅者架构中真正的全局事件实现(例如使用具有许多异步操作的复杂 UI),您希望在其中让任意对象/元素侦听自定义事件。
Through experimentation, I've found that the real answer to why AnC's events were not firing is because jQuery apparently doesn't allow the "." (period) character in custom event names...but underscores seem to be ok.
通过实验,我发现为什么 AnC 的事件没有触发的真正答案是因为 jQuery 显然不允许“.” 自定义事件名称中的(句点)字符...但下划线似乎没问题。
So, if you name your events something like foo_bar (rather than foo.bar), your code should work as expected. Tested with jQuery 1.4.4.
因此,如果您将事件命名为 foo_bar(而不是 foo.bar),您的代码应该可以按预期工作。用 jQuery 1.4.4 测试。
Edit: Just to be clear - I mean that periods aren't allowed for custom events if you want to use the $.event.trigger() mechanism. In scenarios where events are being triggered by objects or elements, periods seem to be ok...not sure if this is a bug or by design.
编辑:只是要清楚 - 我的意思是如果您想使用 $.event.trigger() 机制,则不允许自定义事件的时间段。在事件由对象或元素触发的情况下,周期似乎没问题……不确定这是错误还是设计。

