什么是$(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"?

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

问题描述

我已经使用了一段时间,但大多数时候,我喜欢较短的一个,但是,我只想真正地深入细节。我可能一直在创造buggy代码,我不想贡献和传播懒惰代码在网络上。

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.

非常感谢。

推荐答案

1.3中添加了 live() >在1.7.2中的 on()

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

从1.7 ()是首选使用, live()已被弃用,根本不推荐使用。
如果你使用1.3使用 bind()而不是 live()使用 delegate()而不是 live() / code>

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() 文档


在前两个变体中,此方法是
的快捷方式。 ,处理程序),以及.on(click,处理程序)
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() 文档

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或button,

  2. 使用.on()或.delegate()附加到document.body,
    下面的元素,因为移动iOS在正文中起泡。 / li>
  3. 将CSS样式cursor:指针应用到需要bubble
    点击的元素(或包含document.documentElement的父元素)。注意,
    这将禁用对元素的copy\paste,并触摸时突出显示


  • 调用事件处理程序中的event.stopPropagation()在
    中无效,停止文档中较低的事件处理程序;

  • .live()方法与其他事件方法交互,可以使
    令人惊讶,例如$(document ).unbind(click)删除所有通过任何调用.live()附加的
    处理程序。


  • 其他资源

    < a href =http://api.jquery.com/click/ =nofollow> 点击()

    bind()

    live()(不要使用)

    delegate()

    on()

    Additional Resources
    click()
    bind()
    live() (don't use)
    delegate()
    on()

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

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