jQuery.live 和 jQuery.delegate 之间的区别
我读过一些关于为什么不使用 jQuery.live() 的文章,我想检查一下我是否明白了:) 当我调用 $("body").delegate('element','click', function);
是否与 $(element).live('click', function 相同)
? 在正常行为的情况下..根据帖子,有一些 stopPropagation 和性能增益,但主要区别是每次实时绑定到 body 元素,而委托可以绑定到另一个元素吗?
I have read some post about why do not use jQuery.live() and I want to check if I got it:)
When I call $("body").delegate('element','click', function);
Is it the same as $(element).live('click', function)
?
In case of normal behaviour..According to the post there are some stopPropagation and performance boons, but is the main difference that live bind everytime to body element, while delegate can bind to another one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一个重要的区别是,“.live()”实际上会为初始选择器构建 jQuery 元素列表,即使“.live()”函数本身只需要选择器字符串。这意味着如果选择器有点昂贵,那么设置处理程序的代码将无缘无故地在整个 DOM 上运行。 “.delegate()”调用不这样做。
真的,我看不出新代码应该使用“.live()”的任何理由;这是一种建筑错误,最终应该悄然消亡。
One important difference is that ".live()" will actually build up the jQuery element list for the initial selector, even though the ".live()" function itself only needs the selector string. That means that if the selector is somewhat expensive, the code to set up the handler will go running all over the DOM for no good reason. The ".delegate()" call does not do that.
Really I don't see any reason that new code should use ".live()"; it was sort-of an architectural mistake and should eventually die quietly.
Nettuts 有一个截屏视频只是为了解释这一点:快速提示:Live() 和 Delegate() 之间的区别
引用自网站:
Nettuts has a screencast just to explain this: Quick Tip: The Difference Between Live() and Delegate()
Quote from the site:
是的,完全正确。假设您有一个表,您可以在其中添加和删除行,并且您想要处理对这些行(或行中的链接或按钮)的点击。您可以使用
live
来实现这一点,但是事件必须一直冒泡到主体级别,让我们面对现实吧,它感觉有点像全局变量。如果您在table
元素上使用delegate
,您将保持更有针对性,与页面上发生的其他事情隔离。delegate
是live
的更加模块化、包含的版本。Yes, exactly. Let's say you have a table that you add and remove rows from, and you want to handle clicks on those rows (or links or buttons within the rows). You could use
live
for that, but then the event has to bubble all the way down to the body level and let's face it, it feels a bit like a global variable. If you usedelegate
on thetable
element instead, you remain more targeted, isolated from other things going on on the page.delegate
is a more modular, contained version oflive
.由于 .live() 方法在事件传播到文档顶部后对其进行处理,因此无法停止实时事件的传播。类似地,由 .delegate() 处理的事件将始终传播到它们被委托的元素;当调用委托的事件处理程序时,其下面的任何元素上的事件处理程序都已经执行。
Since the .live() method handles events once they have propagated to the top of the document, it is not possible to stop propagation of live events. Similarly, events handled by .delegate() will always propagate to the element to which they are delegated; event handlers on any elements below it will already have been executed by the time the delegated event handler is called.
其缺点是
.live
在文档级别运行,而.delegate
在您指定的任何元素上运行。为什么它会有所不同?如果您使用.live
绑定了一个(或多个)mousemove 事件,每次您将鼠标移动到页面上的任何位置时,jQuery 都会执行代码,以查看您的回调函数是否应该运行。这是极其低效的,也是使用.delegate
的原因。.delegate
函数仅在偶数源自您指定的 dom 节点内部时运行。例如,如果您说$('ul#myUL').delegate(...)
,那么 jQuery 只会检查当事件源自时代码是否应该运行>ul#myUL
The short of it is that
.live
runs at the document level and.delegate
runs on whatever element you specify. Why does it make a difference? If you have a mousemove event (or several) bound using.live
, jQuery will execute code every time you move your mouse anywhere on the page to see if your callback function should run. This is extremely inefficient and is the reason for having.delegate
..delegate
functions only run when the even originates inside of the dom node you specify. If, for example, you said$('ul#myUL').delegate(...)
, then jQuery would only check to see if the code should run when the event originated from withinul#myUL