jQuery的.live('click',fn)和.click(fn)之间的性能差异 [英] Performance difference between jQuery's .live('click', fn) and .click(fn)

查看:106
本文介绍了jQuery的.live('click',fn)和.click(fn)之间的性能差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢jQuery 1.3中的新的直播活动。我的问题是这个事件的表现。我知道使用实时点击/绑定('点击')的优点,但是在使用它的点击/绑定('点击')上是否有性能打击?



如果没有,为什么要使用点击或绑定(点击)?

解决方案


如果不,你为什么要使用click
或bind('click')?


因为 $。live()有一些显着的缺点



  • 现场活动不会以传统的方式起泡, 不能
    停止使用stopPropagation
    更改jquery 1.4.4)
    stopImmediatePropagation。例如,
    考虑两个点击事件 -
    一个绑定到li和另一个li a。
    如果在内部
    上发生点击,BOTH事件将被触发。
    这是因为当一个
    $(li)。bind(click,fn);是绑定
    你实际上说每当一个
    点击事件发生在LI元素 -
    或LI元素内部 - 触发此
    点击事件。要停止进一步的
    处理即时活动,fn必须
    返回false。

  • 现场活动目前只适用于选择器。对于
    示例,这将工作:$(li
    a)。live(...),但这不会:
    $(a,someElement).live(.. 。)和
    也不会这样:
    $(a)。parent()。live(...)。



I love the new live event in jQuery 1.3. The question I have is the performance of this event. I know the advantages of using live over click/bind('click') but is there a performance hit for using it over click/bind('click')?

If not, why would you ever use click or bind('click')?

解决方案

If not, why would you ever use click or bind('click')?

Because $.live() has some significant disadvantages

  • Live events do not bubble in the traditional manner and cannot be stopped using stopPropagation (This changed in jquery 1.4.4) or stopImmediatePropagation. For example, take the case of two click events - one bound to "li" and another "li a". Should a click occur on the inner anchor BOTH events will be triggered. This is because when a $("li").bind("click", fn); is bound you're actually saying "Whenever a click event occurs on an LI element - or inside an LI element - trigger this click event." To stop further processing for a live event, fn must return false.
  • Live events currently only work when used against a selector. For example, this would work: $("li a").live(...) but this would not: $("a", someElement).live(...) and neither would this: $("a").parent().live(...).

这篇关于jQuery的.live('click',fn)和.click(fn)之间的性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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