on('click')和.click()有什么区别? [英] Different between on('click') and .click()?

查看:93
本文介绍了on('click')和.click()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
jquery on vs click方法

Possible Duplicate:
jquery on vs click methods

我注意到有人说要在最新的jQuery中使用"on",但是有什么区别,我应该使用它吗?

I have noticed some are saying to use "on" in the latest jQuery, but what is the difference and is it true I should use it?

$("#dataTable td").on("click", function(event){
    alert($(this).text());
});

$("#dataTable td").click(function(event){
    alert($(this).text());
});

推荐答案

无.在jQuery源中的此处中查找. .click()函数基本上只是.on('click')的别名,相关代码如下:

None. Look here in the jQuery source. The .click() function is basically just an alias for .on('click'), the relevant code being this:

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

尽管.click()更简洁,但我还是建议您使用 .on 一致,因为它始终旨在统一.bind,.live和.click等速记方法引入的事件处理.

Although .click() is more concise I would recommend using .on if you want to be consistent, as it always was intended to unify event handling introduced with .bind, .live and the shorthand methods like .click.

这篇关于on('click')和.click()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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