jQuery的.bind()与.on() [英] jQuery’s .bind() vs. .on()

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

问题描述

我发现两篇很棒的文章谈论新功能 .on() jquery4u.com elijahmanor.com

I found two great articles talking about the new function .on(): jquery4u.com, elijahmanor.com.

有没有办法 .bind()仍然比 .on()更好?

Is there any way where the .bind() still is better to use than .on()?

例如,我有一个示例代码如下所示:

For example, I have a sample code that look like this:

$("#container").click( function( e ) {} )

你可以注意到我只有选择器检索的一个项目,在我的情况下,< div> 名称 #container 在我的页面加载时已经存在;没有动态添加。重要的是要提到我使用最新版本的jQuery:1.7.2。

You can note that I just have one item retrieved by the selector and in my case, the <div> named #container already exists when my page was loaded; not added dynamically. It’s important to mention that I use the latest version of jQuery: 1.7.2.

对于该样本,应该 .on()代替 .bind()即使我不使用 .on()提供的其他功能功能?

For that sample, should .on() be used instead of .bind() even if I don’t use the other features provided by the .on() function?

推荐答案

在内部, .bind 地图直接到当前版本的jQuery中的 .on 。 (同样适用于 .live 。)因此,如果您使用 .bind ,则会产生微小但实际上无关紧要的性能影响。相反。

Internally, .bind maps directly to .on in the current version of jQuery. (The same goes for .live.) So there is a tiny but practically insignificant performance hit if you use .bind instead.

但是, .bind 可能会在以后的版本中被删除。没有理由继续使用 .bind 而且每个理由都喜欢 .on

However, .bind may be removed from future versions at any time. There is no reason to keep using .bind and every reason to prefer .on instead.

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

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