jQuery live()和liveQuery插件有什么区别? [英] What is the difference between jQuery live() and liveQuery plugin?

查看:79
本文介绍了jQuery live()和liveQuery插件有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题说明了一切。哪一个更好,什么时候使用什么,我从不使用jQuery live(),因为我使用liveQuery插件已有几年了,我已经习惯了它并且仍在继续使用它。但我想知道两者之间的微妙差异以及何时使用它们?

The question says it all. Which one is better and when to use what, I never use jQuery live() as I am using liveQuery plugin for a couple of years, I am used to it and still continue using it. But I want to know the subtle differences between the two and when to use each of it?

推荐答案

live函数原生jQuery利用DOM中的事件冒泡。相比之下,liveQuery插件使用选择器在DOM中查找元素并直接附加事件处理程序。

The "live" function native to jQuery leverages the bubbling of events up the DOM. The "liveQuery" plugin, by contrast, uses the selector to find elements in the DOM and attach event handlers directly.

在我看来,你最好使用live函数在可能的情况下,因为它涉及较少的DOM遍历等。例如,将事件处理程序挂钩到整个大表中的东西对于liveQuery来说可能有点慢,但对于live来说并不慢。可能存在一些问题(当然使用IE)会迫使您有时使用liveQuery,尽管jQuery 1.4已经大大改进了实时。

In my opinion you're way better off using the "live" function when possible, because it involves less DOM traversal etc. For example, hooking event handlers to things throughout a big table can be kind-of slow with liveQuery but not slow at all with "live". There may be some issues (with IE of course) that force you to use liveQuery sometimes, though jQuery 1.4 has improved "live" considerably.

edit 更新:2017年9月

此时,现代版本的jQuery在 .on()中集中了事件处理程序注册 API。简而言之:

At this point, modern versions of jQuery centralize event handler registration in the .on() API. Briefly:

$(selector).live("event-name", handler);

今天会写成 .on()

$(document).on("event-name", selector, handler);

.on() API大幅提供比长期弃用的 .live()方法更灵活,包括使用DOM中的任何节点作为委托点的选项(如旧的 .delegate() did)。

The .on() API provides considerably more flexibility than the long-deprecated .live() method did, including the option of using any node in the DOM as the delegation point (like the old .delegate() did).

这篇关于jQuery live()和liveQuery插件有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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