缺点总是使用live()而不是bind()用于唯一的dom元素? [英] Drawbacks to always using live() instead of bind() for unique dom elements?

查看:143
本文介绍了缺点总是使用live()而不是bind()用于唯一的dom元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写jQuery绑定事件时,我通常使用 bind()别名( click() submit()等)。



但是,我使用动态生成的内容越多,我发现它的含义越多,当 bind()将不工作,最后调试半小时,直到我尝试 live(),它的工作原理。



在ID选择器的参数中(如' #foo ',而不是。课程或元素('input')):



有什么缺点对于这些类型的绑定,除了缺少方便的别名之外,总是使用 live()而不是 bind()因为只能有一个DOM元素绑定到特定的ID?



===========



编辑:我询问 bind() live()这已经被覆盖了。我在问什么是默认使用live()的缺点,因为诱惑是在不能错误地反省的情况下(即,当你使用 #uniqueDomElement ),并避免考虑 bind()是不适合的。

解决方案

.live() 是权重(这适用于使用大量 .live() 处理程序),因为它将事件处理程序附加到文档和事件默认情况下泡沫(整个工作原理),这意味着事件到达文档时,必须检查几件事:




  • 我有任何 .live () 事件类型的事件

  • 如果我这样做,事件来自的元素是否匹配任何选择器 .live() 处理程序?



第一个是相当便宜,第二个不是。让我们来最常见的例子,点击事件。一个点击事件气泡,到目前为止这么好。假设我们有一个或多个 .live() 文档中注册的点击的事件处理程序...现在我们必须循环所有这些处理程序,并比较他们的选择器与事件来自的元素,更多的处理程序,你得到的越贵,并发生在每次点击,这是到目前为止, .live()



还有其他问题,如附加/删除处理程序,但这是处理程序的管理...上述性能问题适用于您有大量的处理程序是将其与直接 .bind() 通话。


When writing jQuery binding events, I typically use the bind() aliases (click(), submit(), etc).

But, the more I use dynamically generated content, the more I find its ambiguous as to when bind() won't work, and end up debugging for a half hour until I try live() and it works.

Within the parameters of ID-selectors (like '#foo', not .classes or elements ('input')):

Are there any drawbacks to just always using live() instead of bind() for these types of bindings, besides the lack of convenient aliases, since there can only be one DOM element tied to a particular ID?

===========

EDIT: I'm not asking what the difference between bind() and live() are; that's been covered. I'm asking what are the drawbacks of just using live() by default, since the temptation is to do so in instances where you can't mistakenly overselect (ie, when you're using a #uniqueDomElement), and avoid thinking about when bind() is not appropriate.

解决方案

The main drawback to .live() is weight (this applies when using a large number of .live() handlers), since it attaches an event handler to document and events by default bubble up there (the entire basis of how it works) that means when the event reaches document a few things have to be checked:

  • Do I have any .live() events for this event type?
  • If I do, does the element the event came from match any selectors for those .live() handlers?

The first is pretty cheap, the second is not. Let's take the most common example, the click event. A click event bubbles, so far so good. Let's say we have one or more .live() event handlers for click registered on document...now we have to loop through all of those handlers and compare their selectors to the element the event came from, the more handlers you have, the more expensive this gets, and happens with every click, that's by far the biggest performance penalty for .live().

There are also other concerns, such as attaching/removing the handlers, but that's management of your handlers...the above performance concerns that apply when you have a large number of handlers are the main issue when comparing it to a direct .bind() call.

这篇关于缺点总是使用live()而不是bind()用于唯一的dom元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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