如何捕获jQuery.event.trigger()? [英] How to catch the jQuery.event.trigger()?

查看:85
本文介绍了如何捕获jQuery.event.trigger()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用未设置为DOM元素的jQuery构建自定义事件。

i want to build a custom event with jQuery that is not set to a DOM element.

它在jQuery描述中说:

It says in the jQuery description, that:

jQuery.event.trigger('test');

是触发自定义事件。

但我怎么能抓住它?

提前thx

推荐答案

我想你问的是那个自定义事件(至少我试图捕获触发器没有意义):用。bind

I suppose you're asking about that custom event (at least it made no sense for me attempting to catch the trigger): it's done with .bind:

$('some_selector').bind('test', function() { 
  console.log('Test was caught'); }
);

更新:其实没有,你不需要选择器 - 只需一个对象成为事件的主持人。您可以使用以下内容:

UPDATE: Actually no, you don't need a selector - just an object to be a 'host' of event. You might use something like this:

// taken from the comments at the Doc page
var o = {length: 3};
$(o).bind( 'custom', function(){ console.log('hi') } );
$(o).trigger('custom');

// Output
hi
hi
hi

这篇关于如何捕获jQuery.event.trigger()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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