Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

javascript - jQuery的.bind()与.on()(jQuery’s .bind() vs. .on())

I found two great articles talking about the new function .on() : jquery4u.com , elijahmanor.com .(我找到了两篇有关新功能.on()精彩文章: jquery4u.comelijahmanor.com 。)

Is there any way where the .bind() still is better to use than .on() ?(有没有什么办法,其中.bind()仍是优于使用.on())

For example, I have a sample code that look like this:(例如,我有一个示例代码,如下所示:)

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

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;(您可能会注意到,选择器只检索了一项,在我的情况下,加载页面时已经存在名为#container<div> 。)

not added dynamically.(没有动态添加。) It's important to mention that I use the latest version of jQuery: 1.7.2.(重要的是要提到我使用的是最新版本的jQuery:1.7.2。)

For that sample, should .on() be used instead of .bind() even if I don't use the other features provided by the .on() function?(对于该示例,即使我不使用.on()函数提供的其他功能,也应该使用.on()代替.bind()吗?)

  ask by Samuel translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Internally, .bind maps directly to .on in the current version of jQuery.(在内部, .bind在当前版本的jQuery中直接映射到.on 。)

(The same goes for .live .) So there is a tiny but practically insignificant performance hit if you use .bind instead.(( .live也是如此。)因此,如果您使用.bind则会对性能产生微小的影响,但实际上微不足道。)

However, .bind may be removed from future versions at any time.(但是, .bind可以随时从将来的版本中删除。)

There is no reason to keep using .bind and every reason to prefer .on instead.(没有理由继续使用.bind ,也没有理由更倾向于使用.on 。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...