$(sel).bind("click", $(sel).click(, $(sel).live("click", $(sel).on("click"? [英] What are the significant differences among $(sel).bind("click", $(sel).click(, $(sel).live("click", $(sel).on("click"?

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

问题描述

我已经使用它们很长一段时间了,但大多数时候,我更喜欢较短的,但是,我只是想真正深入了解细节.我可能一直在创建错误的代码,我不想在网络上贡献和传播懒惰的代码.

I've been using them for quite some time, but most of the time, I prefer the shorter one, however, I just want to really dig in to the nitty-gritty details. I may have been creating buggy codes and I don't want to contribute and spread lazily-done codes out in the web.

那么,告诉我:

它们之间有哪些显着的优点/缺点,或者是就像冰淇淋一样,不同的口味,但同样的好感"效果?

What are the significant advantages/disadvantages among them, or is it just like ice cream, different flavors but same "feel-good" effect?

我们鼓励每个人就此事发表他们的专家意见.

Everyone is encouraged to throw their expert opinions regarding this matter.

非常感谢.

推荐答案

bind() 1.0 新增,live() 1.3,delegate() 在 1.4.2 和 on() 在 1.7.

bind() was added in 1.0, live() in 1.3, delegate() in 1.4.2 and on() in 1.7.

从 1.7 开始,on() 是首选用途,而 live() 已弃用且根本不推荐使用.如果您使用的是 1.3,请使用 bind() 而不是 live() 并且从 1.4.2 开始使用 delegate() 而不是 live() 并且从 1.7 开始使用 on() 而不是其他任何一个.

As of 1.7 on() is the preferred use and live() is deprecated and not recommended at all. If you are using 1.3 use bind() instead of live() and as of 1.4.2 use delegate() instead of live() and as of 1.7 use on() instead of any of the others.

关于$("selector").click.取自 click() documentation:

Regarding $("selector").click. Taken from the click() documentation:

在前两个变体中,此方法是.bind("click", handler) 和 for .on("click", handler) 一样jQuery 1.7.在第三个变体中,当 .click() 被调用时参数,它是 .trigger("click") 的快捷方式.

In the first two variations, this method is a shortcut for .bind("click", handler), as well as for .on("click", handler) as of jQuery 1.7. In the third variation, when .click() is called without arguments, it is a shortcut for .trigger("click").

为什么使用 on() 而不是其他的?
on() 是最新添加的,在 1.7 版中加入了 jQuery 库.on() 有几个方法签名,使其能够提供与以前版本相同的结果,但经过改进和优化.引用 文档:

Why use on() instead of the others?
on() is the latest addition, joining the jQuery library in version 1.7. on() has several method signatures enabling it to deliver the same results previous version do but improved and optimised. To quote from the documentation:

从 jQuery 1.7 开始,.on() 方法提供了所有需要的功能用于附加事件处理程序.

As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers.

基本上不再需要使用 bind()delegate() 了.当然它会起作用,并且使用这些方法应该没有害处,但我总是认为最新添加的内容已针对以前版本的任何缺点进行了优化和改进(除非文档另有说明,因为它是 <代码>live()).
基于此,我建议改用 on().

There is bascialy no need to use bind() or delegate() anymore. Sure it will work and there should be no harm in using those methods but I would always assume that the latest additions are optimised and improved on any of the drawbacks of previous versions (unless otherwise stated by the documentation as it is in the case of live()).
Based on that I would recommend to use on() instead.

live() 不推荐使用句号的原因更多是因为它的缺点.引用 live() documentation.

The reason live() is not recommended full-stop is more to do with it's drawbacks. To quote from the live() documentation.

以后不再推荐使用 .live() 方法jQuery 的版本提供了更好的方法,没有它的缺点.特别是,以下问题与使用.live():

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() 方法之前,这可能很耗时大型文档.
  • 不支持链接方法.例如, $("a").find(".offsite, .external").live( ... );无效并且确实没有按预期工作.
  • 由于所有 .live() 事件都附加在文档元素上,因此事件在处理之前采取最长和最慢的路径.
  • 在移动 iOS(iPhone、iPad 和 iPod Touch)上,点击事件不会大多数元素的文档正文冒泡,不能与.live() 而不应用以下解决方法之一:
  • jQuery attempts to retrieve the elements specified by the selector before calling the .live() method, which may be time-consuming on large documents.
  • Chaining methods is not supported. For example, $("a").find(".offsite, .external").live( ... ); is not valid and does not work as expected.
  • Since all .live() events are attached at the document element, events take the longest and slowest possible path before they are handled.
  • On mobile iOS (iPhone, iPad and iPod Touch) the click event does not bubble to the document body for most elements and cannot be used with .live() without applying one of the following workarounds:
  1. 使用原生可点击元素,例如 a 或按钮,因为这两者冒泡记录.
  2. 使用 .on() 或 .delegate() 附加到 document.body 级别以下的元素,因为移动 iOS 确实会在体内冒泡.
  3. 应用 CSS 样式 cursor:pointer 指向需要冒泡的元素点击(或包括 document.documentElement 的父级).但是请注意,这将禁用元素上的复制粘贴并使其成为触摸时突出显示.

  • 在事件处理程序中调用 event.stopPropagation() 是无效的停止附加在文档下方的事件处理程序;活动有已传播到文档.
  • .live() 方法与其他事件方法的交互方式可以令人惊讶,例如, $(document).unbind("click") 删除所有点击通过对 .live() 的任何调用附加的处理程序!
  • 不过,文档中还有很多好东西.

    There is a lot more goodies in the documentation though.

    其他资源
    click()
    bind()
    live()(不要使用)
    delegate()
    on()

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

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