Javascript jQuery .trigger() 多个事件

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

jQuery .trigger() multiple events

javascriptjquerytriggers

提问by Zach Wolf

I'm writing a jQuery plugin and using .onand .triggeras my pub/sub system. However, I want to trigger multiple events in different scenarios.

我正在编写一个 jQuery 插件并使用.on.trigger作为我的发布/订阅系统。但是,我想在不同的场景中触发多个事件。

Is this possible to do as one string, like the .onmethod?

这可以像.on方法一样作为一个字符串来做吗?

Goal:

目标:

$this.trigger("success next etc");    // doesn't work

Current solution:

当前解决方案:

$this
    .trigger("success")
    .trigger("next")
    .trigger("etc");                  // works, triggers all three events

Any suggestions?

有什么建议?

回答by hazzik

JQuery itself does not support triggering multiple events, however you could write custom extension method triggerAll

JQuery 本身不支持触发多个事件,但是您可以编写自定义扩展方法 triggerAll

(function($) {
    $.fn.extend({
        triggerAll: function (events, params) {
            var el = this, i, evts = events.split(' ');
            for (i = 0; i < evts.length; i += 1) {
                el.trigger(evts[i], params);
            }
            return el;
        }
    });
})(jQuery);

And call it like following:

并像下面这样调用它:

$this.triggerAll("success next etc");

回答by Derek Hunziker

What you have is fine... you can't trigger multiple events using a comma separated list. The trigger() constructor only takes an event name and optional additional parameters to pass along to the event handler.

你所拥有的很好......你不能使用逗号分隔的列表触发多个事件。trigger() 构造函数只接受一个事件名称和可选的附加参数来传递给事件处理程序。

An alterternative would be to trigger all events attached to an element, however, this may not meet your needs if you need to trigger specific events in different senarios:

另一种选择是触发附加到元素的所有事件,但是,如果您需要在不同的场景中触发特定事件,这可能无法满足您的需求:

$.each($this.data('events'), function(k, v) {
    $this.trigger(k);
});?

回答by Zach Wolf

Just in case anyone else stumbles upon this question later in life, I solved this by creating a custom jQuery function.

以防万一其他人在以后的生活中偶然发现这个问题,我通过创建一个自定义的 jQuery 函数解决了这个问题。

$.fn.triggerMultiple    =   function(list){
    return this.each(function(){
        var $this = $(this); // cache target

        $.each(list.split(" "), function(k, v){ // split string and loop through params
            $this.trigger(v); // trigger each passed param
        });
    });
};

$this.triggerMultiple("success next etc"); // triggers each event

回答by deviprsd

Since, I don't have the privilege of commenting, so I would like to point out that $this.data('events') doesn't work, the inner use function has to be used. $._data($this[0], 'events')

因为,我没有评论的权限,所以我想指出$this.data('events') doesn't work,必须使用内部使用功能。$._data($this[0], 'events')