javascript 如何捕捉 jQuery.event.trigger()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11132553/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:09:37  来源:igfitidea点击:

How to catch the jQuery.event.trigger()?

javascriptjquery

提问by Moszeed

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

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

It says in the jQuery description, that:

它在 jQuery 描述中说:

jQuery.event.trigger('test');

is to trigger an Custom Event.

是触发自定义事件。

But how i can catch it ??

但我怎么能抓住它?

thx in advance

提前谢谢

回答by raina77ow

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:

我想你是在问那个自定义事件(至少我试图抓住它没有意义trigger):它是用.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

回答by Judah Anthony

I think you guys are missing the question. He is not asking how you listen for an event triggered on an object. He is asking how you listen for an event triggered directly through jQuery.event.trigger().

我认为你们错过了这个问题。他不是在问你如何监听在对象上触发的事件。他问你如何监听直接通过 jQuery.event.trigger() 触发的事件。

I found a really great article at http://www.sitepoint.com/jquery-custom-events/.

我在http://www.sitepoint.com/jquery-custom-events/找到了一篇非常棒的文章。

Essentially you just listen on the document object.

本质上,您只是在听文档对象。

$(document).on("test", function(event, data) {
  console.log(data)
});

$.event.trigger('test', [{ foo: 'bar' }]);

回答by yanliang_alex

you should pass the target element of the event to jQuery.event.trigger(), like this:

您应该将事件的目标元素传递给 jQuery.event.trigger(),如下所示:

jQuery.event.trigger('test', null, element);

so that when you want to catch it, you can use :

这样当你想抓住它时,你可以使用:

$(element).on('test', fn);

You can find the definition and usage of jQuery.event.trigger in the jQuery source:

jQuery.event.trigger的定义和用法可以在jQuery源码中找到:

After reading source code of jQuery, you'd find the definition of jQuery.event.trigger:

阅读jQuery的源代码后,你会发现jQuery.event.trigger的定义:

jQuery.event = {
    ...

    trigger: function( event, data, elem, onlyHandlers ) {...}

    ...
}

(this is not meant to be a public interface of jQuery and just for internal use.)

(这不是 jQuery 的公共接口,仅供内部使用。)

and the usage:

和用法:

jQuery.fn.extend({
...
    trigger: function( type, data ) {
        return this.each(function() {
            jQuery.event.trigger( type, data, this );
        });
    },
    triggerHandler: function( type, data ) {
        var elem = this[0];
        if ( elem ) {
            return jQuery.event.trigger( type, data, elem, true );
        }
    },
...
});

回答by Miscreant

Here's how:

就是这样:

$(document).bind('test', function (e) {
  // handler code here...
});