jQuery .live() 和 .on() 有什么区别
我看到 jQuery 1.7 中有一个新方法 .on()
,它取代了早期版本中的 .live()
。
我很想知道它们之间的区别以及使用这种新方法的好处是什么。
I see there's a new method .on()
in jQuery 1.7 that replaces the .live()
in earlier versions.
I'm interested to know the difference between them and what the benefits are of using this new method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
docs 中非常清楚为什么您不想使用 live。另外,正如 Felix 所提到的,
.on
是一种更简化的附加事件的方式。It's pretty clear in the docs why you wouldn't want to use live. Also as mentioned by Felix,
.on
is a more streamline way of attaching events.人们从
.live()
迁移到.on()
时偶然发现的一个区别是.on()
的参数略有不同将事件绑定到动态添加到 DOM 的元素时有所不同。下面是我们用于
.live()
方法的语法示例:现在,
.live()
在 jQuery 版本 1.7 中已弃用,并在版本 1.9 中删除,您应该使用.on()
方法。下面是使用.on()
方法的等效示例:请注意,我们是针对文档而不是按钮本身调用
.on()
。我们在第二个参数中指定我们正在侦听其事件的元素的选择器。在上面的示例中,我在文档上调用
.on()
,但是如果您使用更靠近选择器的元素,您将获得更好的性能。只要在调用.on()
之前页面上存在任何祖先元素,它就可以工作。文档中对此进行了解释,但很容易被忽略。
One difference that people stumble on when moving from
.live()
to.on()
is that the parameters for.on()
are slightly different when binding events to elements added dynamically to the DOM.Here's an example of the syntax we used to use with the
.live()
method:Now with
.live()
being deprecated in jQuery version 1.7 and removed in version 1.9, you should use the.on()
method. Here's an equivalent example using the.on()
method:Please note that we're calling
.on()
against the document rather than the button itself. We specify the selector for the element whose events we're listening to in the second parameter.In the example above, I'm calling
.on()
on the document, however you'll get better performance if you use an element closer to your selector. Any ancestor element will work so long as it exists on the page before you call.on()
.This is explained here in the documentation but it is quite easy to miss.
请参阅官方博客
See the official Blog
此方法用于为现在和将来与当前选择器匹配的所有元素附加事件处理程序。
此
方法用于将一个或多个事件的事件处理函数附加到所选元素,下面是示例。
This method is used to attach an event handler for all elements which match the current selector, now and in the future.
and
This method is used to attach an event handler function for one or more events to the selected elements below is the example.
关于在线与实时之间差异的好教程
从上面的链接
Good tutorial on difference between on vs live
Quote from the above link
有关更多信息,请查看.. .live() 和 .on()
.live() 方法在处理内容的动态生成时使用...就像我在程序中创建的那样当我更改 Jquery Slider 的值并且我想附加关闭时的选项卡将生成的每个选项卡的按钮功能...我尝试过的代码是..
并且它的工作原理非常酷...
for more info check it out.. .live() and .on()
.live() method is used when you deal with the dynamic generation of contents... like I have created on program that adds a tab when i change the value of a Jquery Slider and I want to attach the close button functionality to every tabs which will generated... the code i have tried is..
and it works preety much cool...
我是使用 jQuery 的 Chrome 扩展程序“评论保存”的作者,以及使用
.live()
的一个。该扩展的工作方式是使用 .live()
将侦听器附加到所有文本区域 - 这效果很好,因为每当文档更改时,它仍然会将侦听器附加到所有新的文本区域。我移至
.on()
但它不起作用。每当文档发生更改时,它都不会附加侦听器 - 因此我已恢复使用.live()
。我猜这是.on()
中的一个错误。我想只是要小心一点。I am the author of a Chrome extension "Comment Save" which uses jQuery, and one which used
.live()
. The way the extension works is by attaching a listener to all the textareas using .live()
- this worked well since whenever the document changed it would still attach the listener to all the new textareas.I moved to
.on()
but it doesn't work as well. It doesn't attach the listener whenever the document changes - so I have reverted back to using.live()
. That is a bug I guess in.on()
. Just be careful about it I guess.我需要识别浏览器关闭事件。在进行研究之后,我正在使用 jQuery 1.8.3
单击超链接时使用以下 jQuery 打开标志
$('a').live('click', function() {cleanSession = false;});
每当单击提交的输入按钮类型时,使用以下 jQuery 打开一个标志
$("input[type=submit] ").live('click',function(){alert('输入按钮被点击');cleanSession=false;});
$('form').live('submit', function() {cleanSession = false;});
现在重要的是......我的解决方案只有在我使用 .live 而不是 .on 时才有效。如果我使用 .on 那么该事件会在表单提交后被触发,但为时已晚。很多时候,我的表单是使用 javascript 调用 (document.form.submit) 提交的,
因此 .live 和 .on 之间存在关键区别。如果您使用 .live,您的事件会立即触发,但如果您切换到 .on,它不会按时触发
I have a requirement to identify browser closed event. After Doing to of research I am doing following using jQuery 1.8.3
Turn ON a flag using following jQuery when hyperlink is clicked
$('a').live('click', function() {cleanSession = false;});
Turn ON a flag using following jQuery when any time input button type of submit is clicked
$("input[type=submit]").live('click',function(){alert('input button clicked');cleanSession=false;});
$('form').live('submit', function() {cleanSession = false;});
Now important thing...my solution only works if I use .live instead .on. If I use .on then the event gets fired after form gets submitted and which is too late. Many times my forms gets submitted using javascript call (document.form.submit)
So there is a key difference between .live and .on . If you use .live your events get fired immediately but if you switch to .on it doesn't get fired on time