所有侦听器上的jQuery事件触发
我有一个自定义事件,我想在某个时候触发该事件,而该事件与选择无关.
I have a custom event I've made which I would like to trigger at some point with no relevance to selection.
IE-我想做一些可以像运行
I.E - I would like to do something that would behave as running
$("*").trigger('customEvent');
但是jQuery文档警告说,使用通用选择器非常慢.
有没有一种方法可以触发绑定到特定事件的所有对象,而不必使用通用选择器$("*")
?
But jQuery documentation warns that using the universal selector is very slow.
Is there a way to trigger all the object that are bound to a specific event without having to use the universal selector $("*")
?
谢谢!
P.S-我目前正在使用称为custom_event_listener
的特定class
,并使用$('.custom_event_listener').trigger('customEvent')
以避免使用通用选择器.我想知道是否有一种避免使用class
的方法.
P.S - I'm currently using a specific class
called custom_event_listener
and use $('.custom_event_listener').trigger('customEvent')
to avoid using a universal selector. I'm wondering if there is a way to avoid the use of a class
.
您可以在所有绑定了这样的处理程序的事件上触发事件:
You can trigger an event on everything that has a handler bound like this:
$.event.trigger('customEvent');
此循环通过$.cache
查找真正具有处理程序的对象,然后在这些元素上触发 ...而不是仅仅找到每个元素并在每个元素上触发事件.
This loops through $.cache
to find what actually has a handler, then fires on those elements...rather than just finding every element and firing the event on each one.