.trigger() 与 .click() 中的 jQuery 优势/差异

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

jQuery advantages/differences in .trigger() vs .click()

jquery

提问by benhowdle89

In terms of performance, what are the gains (or just differences) between:

在性能方面,以下之间的收益(或只是差异)是什么:

$('.myEl').click();

and

$('.myEl').trigger('click');

Are there any at all?

有吗?

采纳答案by andlrc

This is the code for the clickmethod:

这是该click方法的代码

jQuery.fn.click = function (data, fn) {
    if (fn == null) {
        fn = data;
        data = null;
    }

    return arguments.length > 0 ? this.on("click", null, data, fn) : this.trigger("click");
}

as you can see; if no arguments are passed to the function it will trigger the click event.

如你看到的; 如果没有参数传递给函数,它将触发点击事件。



Using .trigger("click")will call one less function.

使用.trigger("click")将调用少一个函数。

And as @Sandeep pointed out in his answer.trigger("click")is faster:

正如@Sandeep 在他的回答中指出的那样.trigger("click")更快:



As of 1.9.0 the check for dataand fnhas been moved to the .onfunction:

从 1.9.0 开始,检查datafn已移至.on函数

$.fn.click = function (data, fn) {
    return arguments.length > 0 ? this.on("click", null, data, fn) : this.trigger("click");
}

回答by Nicola Peluchetti

i think that

我觉得

$('.myEl').trigger('click');

is better because it saves you a function call as $('.myEl').click();just calls that funciton. Look at the code from jQuery source

更好,因为它为您节省了函数调用,$('.myEl').click();就像调用该函数一样。查看来自jQuery源的代码

jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblclick " +
    "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " +
    "change select submit keydown keypress keyup error contextmenu").split(" "), function( i, name ) {

    // Handle event binding
    jQuery.fn[ name ] = function( data, fn ) {
        if ( fn == null ) {
            fn = data;
            data = null;
        }

        return arguments.length > 0 ?
            this.on( name, null, data, fn ) :
                    //here they call trigger('click'); if you provide no arguments
            this.trigger( name );
    };

    if ( jQuery.attrFn ) {
        jQuery.attrFn[ name ] = true;
    }

    if ( rkeyEvent.test( name ) ) {
        jQuery.event.fixHooks[ name ] = jQuery.event.keyHooks;
    }

    if ( rmouseEvent.test( name ) ) {
        jQuery.event.fixHooks[ name ] = jQuery.event.mouseHooks;
    }
});

回答by sandeep

for performance kind .check here.. http://jsperf.com/trigger-vs-not-triggerBoth are almost same...click() is shorthand of trigger('click').

对于性能种类。在这里检查.. http://jsperf.com/trigger-vs-not-trigger两者几乎相同...click() 是 trigger('click') 的简写。

回答by Pawel Zubrycki

Check http://api.jquery.com/click/:

检查http://api.jquery.com/click/

In the third variation, when .click() is called without arguments, it is a shortcut for .trigger("click").

在第三种变体中,当不带参数调用 .click() 时,它是 .trigger("click") 的快捷方式。

It seems they are the same.

似乎他们是一样的。