所有侦听器上的jQuery事件触发 [英] jQuery event triggering on all listeners

查看:72
本文介绍了所有侦听器上的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.

这篇关于所有侦听器上的jQuery事件触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆