在jQueryv1.7中添加了一个新方法on.从文档中可以看到:

‘The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers.’

livebind有什么不同?

推荐答案

on()是将jQuery的大多数事件绑定函数合并为一个函数的try .这还有一个额外的好处,那就是用livedelegate来整理效率低下的地方.在future 的jQuery版本中,这些方法将被删除,只剩下onone.

例如:

// Using live()
$(".mySelector").live("click", fn);

// Equivalent `on` (there isn't an exact equivalent, but with good reason)
$(document).on("click", ".mySelector", fn);
// Using bind()
$(".mySelector").bind("click", fn);

// Equivalent `on`
$(".mySelector").on("click", fn);
// Using delegate()
$(document.body).delegate(".mySelector", "click", fn);

// Equivalent `on`
$(document.body).on("click", ".mySelector", fn);

Internally, jQuery maps all these methods and shorthand event handler setters to the on() method, further indicating that you should ignore these methods from now on and just use on:

bind: function( types, data, fn ) {
    return this.on( types, null, data, fn );
},
live: function( types, data, fn ) {
    jQuery( this.context ).on( types, this.selector, data, fn );
    return this;
},
delegate: function( selector, types, data, fn ) {
    return this.on( types, selector, data, fn );
},

https://github.com/jquery/jquery/blob/1.7/src/event.js#L965.

Jquery相关问答推荐

当父迪瓦是重复元素时,如何将一些子元素包裹在父迪瓦中

如何用另一个响应替换窗口的 URL 哈希?

AngularJS 中的 ScrollTo 函数

使用 jQuery 按文本内容 Select 选项

JavaScript 中的循环计时器

jQuery 中的 $this 与 $(this)

jquery - 单击事件不适用于动态创建的按钮

如何在 Jquery 中通过索引获取子元素?

如何通过 Select 插件使用 jQuery 验证?

javascript中的描述关键字

如何禁用在div内单击

jQuery淡出不显示无?

jQuery 判断 是否存在并且有一个值

动态创建并提交表单

如何推迟内联 Javascript?

JQuery:如果 div 可见

捕获在页面任意位置按下的 Enter 键

Rails 无法正确解码来自 jQuery 的 JSON(数组变成带有整数键的散列)

jQuery vs jQuery Mobile vs jQuery UI?

如何判断值是否为 JSON 对象?