.trigger()与.click()的jQuery优势/差异 [英] jQuery advantages/differences in .trigger() vs .click()

查看:96
本文介绍了.trigger()与.click()的jQuery优势/差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就性能而言,以下各项之间有哪些收益(或仅仅是区别)?

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

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

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

有没有?

推荐答案

这是click方法的代码:

This is the code for the click method:

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");
}

如您所见

如果未将任何参数传递给该函数,它将触发click事件.

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

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

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

正如@Sandeep在他的 answer .trigger("click")中指出的那样,速度更快:

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

从1.9.0版开始,对datafn已移至.on功能:

As of 1.9.0 the check for data and fn has been moved to the .on function:

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

这篇关于.trigger()与.click()的jQuery优势/差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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