jQuery 的 .live('click', fn) 和 .click(fn) 之间的性能差异 [英] Performance difference between jQuery's .live('click', fn) and .click(fn)
本文介绍了jQuery 的 .live('click', fn) 和 .click(fn) 之间的性能差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我喜欢 jQuery 1.3 中的新直播活动.我的问题是这次活动的表现.我知道使用 live 优于 click/bind('click') 的优点,但是使用它而不是 click/bind('click') 会降低性能吗?
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')?
如果不是,你为什么要使用 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')?
因为$.live() 有一些明显的缺点
Because $.live() has some significant disadvantages
- 现场活动不会以传统方式冒泡,
也不能停止使用 stopPropagation(这在 jquery 1.4.4 中有所改变) 或停止立即传播.例如,以两个点击事件为例 -一个绑定到li",另一个绑定到li a".如果内部发生点击锚点两个事件都会被触发.这是因为当一个$("li").bind("click", fn);被绑定你实际上是在说每当单击事件发生在 LI 元素上 -或在 LI 元素内 - 触发此单击事件." 进一步停止处理实时事件,fn 必须返回错误. - 实时事件目前仅在针对选择器使用时才有效.为了例如,这会起作用: $("lia").live(...) 但这不会:$("a", someElement).live(...) 和这也不会:$("a").parent().live(...).
- 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屋!
查看全文