jQuery.bind()和jQuery.on()之间有什么区别? [英] What's the difference between jQuery.bind() and jQuery.on()?

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

问题描述

为什么.on()现在在jQuery 1.7中更受欢迎?

And why is .on() now preferred in jQuery 1.7?

推荐答案

.on( )现在提供 .live() .delegate()和<的组合code> .bind()所有在一个统一的方法。您可以通过如何使用 .on()的参数来获取这三者中的任何一个的行为。

.on() now offers a combination of .live(), .delegate() and .bind() all in one unified method. You can get the behavior of any of those three just by how you use the arguments to .on().

这些对在功能上是相同的:

These pairs are functionally the same:

// events bound directly to the object they occur on
$('.button').on('click', fn);
$('.button').bind('click', fn);

// events intercepted after bubbling up to a common parent object
$('.container').on("click", '.button', fn);
$('.container').delegate('.button', "click", fn);

更多信息在 jQuery博客条目

在统一这些单独的功能之前, jQuery有多种不同的实现。现在, .on()是超集函数, .bind() .live () .delegate()所有只需在其实现中调用 .on()所以现在只有一个实际事件处理的实现。因此,从这个角度来看,它也是一个代码清理和简化问题。同样, .die() .undelegate() .unbind()现在只需调用 .off()而不是单独实现。

Before unifying these separate functions, jQuery had multiple different implementations. Now, .on() is the superset function and .bind(), .live() and .delegate() all just call .on() in their implementation so there is now only one implementation of the actual event handling. So, from that standpoint, it was also a code cleanup and streamlining issue. Similarly, .die(), .undelegate() and .unbind() just call .off() now rather than have separate implementations.

注意: .live()已被弃用于所有版本的jQuery,因为它只是拦截文档对象上所有冒泡事件的特殊情况,因此可以很容易地用<$ c替换它$ c> .delegate()或 .on()当许多事件都在文档对象上处理时,它可能会成为一个性能问题检查每个事件的很多选择器。将像这样的委托事件挂钩到更接近事件发生位置的公共父级比将它们全部放在文档对象上要高效得多(因此为什么 .live()不好用)。

Note: .live() has been deprecated for all versions of jQuery because it's just a special case of intercepting all the bubbled events on the document object so it can be easily replaced with either .delegate() or .on() and when lots of events were all being handled on the document object, it could become a performance problem checking lots of selectors on every event. It's much more efficient to hook a delegated event like this to a common parent that is much closer to where the event occurs than put them all on the document object (thus why .live() is not good to use).

从jQuery 1.7源代码,您可以看到所有这些函数现在如何调用 .on() .off()

From the jQuery 1.7 source, you can see how all these functions just now call .on() and .off():

bind: function( types, data, fn ) {
    return this.on( types, null, data, fn );
},
unbind: function( types, fn ) {
    return this.off( types, null, fn );
},

live: function( types, data, fn ) {
    jQuery( this.context ).on( types, this.selector, data, fn );
    return this;
},
die: function( types, fn ) {
    jQuery( this.context ).off( types, this.selector || "**", fn );
    return this;
},

delegate: function( selector, types, data, fn ) {
    return this.on( types, selector, data, fn );
},
undelegate: function( selector, types, fn ) {
    // ( namespace ) or ( selector, types [, fn] )
    return arguments.length == 1? this.off( selector, "**" ) : this.off( types, selector, fn );
},

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

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