jQuery事件处理程序性能 [英] jquery event handler performance
问题描述
我有几个这样绑定的事件处理程序:
I have several event handlers that I bind like this:
$('#MyDiv').click(function () {...});
$('#SomeDiv').mouseenter(function () {...});
这些处理程序绑定在document.ready函数中,以后不需要重新绑定,因为HTML始终保持不变.切换到.bind()函数或版本1.7中较新的.on(),性能会有所提高.
These handlers are bound in the document.ready function and don't need to be rebound later because the HTML always stays the same. Will there be any performance gains by switching to the .bind() function or the newer .on() found in version 1.7.
感谢您的建议.
推荐答案
以下是使用jsperf的测试: http://jsperf.com/jquery-click-vs-bind-vs-on .事实证明on
是最快的.
Here is a test using jsperf: http://jsperf.com/jquery-click-vs-bind-vs-on. Turns out that on
is the fastest.
刚刚意识到我在设置中有一个错误.对不起,它们具有相同的性能. http://jsperf.com/jquery-click-vs-bind-vs -on/2
Just realized that I had an error in the setup. They have the same performance, sorry. http://jsperf.com/jquery-click-vs-bind-vs-on/2
这篇关于jQuery事件处理程序性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!