开发者

jQuery event triggering on all listeners

开发者 https://www.devze.com 2023-03-23 18:56 出处:网络
I have a custom event I\'ve made which I would like to trigger at some point with no relevance to selection.

I have a custom event I've made which I would like to trigger at some point with no relevance to selection.

I.E - I would like to do something that would behave as running

$("*").trigger('customEvent');

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 $("*")?

Thanks!

P.S - I'm currently using a specific class called custom_event_listener and use $('.custom_eve开发者_如何转开发nt_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'); 

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消