.bind()或.click()更好地绑定单击事件处理程序吗? [英] Is .bind() or .click() better to bind a click event handler?

查看:151
本文介绍了.bind()或.click()更好地绑定单击事件处理程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要将click事件处理程序添加到元素,.bind('click').click更好吗?有什么区别?有性能方面的内容吗?

To add a click event handler to an element, is .bind('click') or .click better? What makes the difference? Any performance aspects?

推荐答案

没有区别.在内部,click仅调用on,而bind也仅调用on.因此,对于极小的速度提升,只需使用 on :

There is no difference. Internally, click just calls on, and bind also just calls on. So for a very minor speed increase, just use on:

$("#someId").on("click", function () {
    //Do stuff
});

这是jQuery 1.7.2的相关部分 .click()方法的源:

Here's the relevant part of the jQuery 1.7.2 source for the .click() method:

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

以及.bind()方法的:

return this.on(types, null, data, fn);


如果您使用的是1.7以下版本的jQuery ...

请注意,.on()方法是jQuery 1.7中引入的.如果您使用的是旧版本,则.click()在内部只会调用.bind().这是来自1.6.2的.click()源. /a>:

Note that the .on() method was introduced in jQuery 1.7. If you are using an older version, .click() will internally just call .bind() instead. Here's the source of .click() from 1.6.2:

return arguments.length > 0 ? this.bind(name, data, fn) : this.trigger(name);

这篇关于.bind()或.click()更好地绑定单击事件处理程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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