我看到jQuery1.7中有一个新的方法.on(),它取代了早期版本中的.live().

I'm interested to know the difference between them and what the benefits are of using this new method.

推荐答案

It's pretty clear in the docs why you wouldn't want to use live. Also as mentioned by Felix, .on is a more streamline way of attaching events.

Use of the .live() method is no longer recommended since later versions of jQuery offer better methods that do not have its drawbacks. In particular, the following issues arise with the use of .live():

  • jQuery在调用.live()方法之前try 检索 Select 器指定的元素,该方法可能是
  • Chaining methods is not supported. For example, $("a").find(".offsite, .external").live( ... ); is not valid and does not work as expected.
  • 由于所有.live()个事件都附加在document元素上,因此事件花费的时间最长、速度最慢
  • Calling event.stopPropagation() in the event handler is ineffective in stopping event handlers attached lower in the document; the event has already propagated to document.
  • .live()方法以令人惊讶的方式与其他事件方法交互,例如, $(document).unbind("click")删除所有单击处理程序 任何拨打.live()的电话都可以!

Jquery相关问答推荐

如何用each替换元素的attr href

我在自定义 WordPress 插件中使用 ajax 时收到错误请求

使用 shell 脚本判断 json 数组响应是否具有特定用户名和状态的 jq 命令

将一个类动态添加到 Bootstrap 的popover容器中

如何使用 jQuery 获取所有 ID?

$.each() 与 for() 循环 - 和性能

jQuery/JavaScript 碰撞检测

在 jQuery 中将 JSON 数组转换为 HTML 表

如何从表格单元格 (td) 中获取相应的表格标题 (th)?

使用 jQuery Select 最后 5 个元素

使用 jQuery DataTables 时禁用第一列的自动排序

你如何在Javascript中缓存图像

jquery: $(window).scrollTop() 但没有 $(window).scrollBottom()

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

从 Select 元素中获取选定的选项

检测夹点的最简单方法

CORS - 如何预检httprequest?

将多个参数传递给 jQuery ajax 调用

如何使用 jQuery Select 第一个父 DIV?

如何使用 jQuery 的 form.serialize 但排除空字段