.on()或.click()哪个更快? [英] Which is faster, .on() or .click()?

查看:153
本文介绍了.on()或.click()哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个带有删除记录链接的表,这是链接click事件的最佳方法?这些对引擎盖的评价是否相同?

If I have a table with links to delete a record, which is the best way to wire up the click event? Do these evaluate the same under the hood?

$("#table").on("click", ".delete", function(){
   //do stuff
});

$("#table .delete").click(function(){
   //do stuff
});

推荐答案

不,他们没有.

以您使用它们的方式...

In the way you are using them...

.on() 您使用它的方式(委托),可以节省附件和内存的周期,因为它实际上并没有附加事件,因为它提供了一个动态元素,该事件也将应用于以后的元素.

.on() The way you are using it (delegation), will save you cycles at attachment as well as memory, because it doesn't actually attach the events in that moment, as it provides a dynamic element where the event is applied to future elements as well.

.click()将在执行时节省您的周期,因为它会在该时刻将事件显式地附加到每个匹配的元素上,但是实际上,这将是我寻找瓶颈的最后一个地方.

.click() will save you cycles at execution, as it explicitly attaches an event to every matching element at that moment in time, but in reality, this would be the last place I'd look for a bottleneck.

在此处阅读一些有关事件委托的测试...

附加事件处理程序的性能测试...

这篇关于.on()或.click()哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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