简体   繁体   English

是否更好地使用.delegate()性能?

[英]Is it better to use .delegate() performance wise?

One of the developers I work with began to write all his code this way: 我工作的一个开发人员开始以这种方式编写所有代码:

$('.toggles').delegate('input', 'click', function() { 
   // do something  
});

vs: VS:

$('.toggles').click(function() { 
   // do something  
});

Are there any performance benefits to doing this? 这样做有什么性能上的好处吗?

delegate() is superseded as of jQuery 1.7. delegate()从jQuery 1.7开始被取代。

Use .on() instead. 请改用.on()


.on() has excellent performance benchmarks. .on()具有出色的性能基准。 And covers your .click() needs as well as needed 并涵盖您的.click()需求以及所需

As frenchie stated, in the latest version of jQuery, both functions end up mapping to jQuery.on . 正如frenchie所说,在最新版本的jQuery中,两个函数最终映射到jQuery.on

In the jQuery code, you can see that delegate is really just a wrapper for on : 在jQuery代码中,您可以看到委托实际上只是on的包装器:

delegate: function( selector, types, data, fn ) {
    return this.on( types, selector, data, fn );
},

Then, when jQuery binds click (as well as other events) it calls into on as well: 然后,当jQuery绑定click (以及其他事件)时,它也会调用on

jQuery.each( ("... click ... ").split(" "), function( i, name ) {

// Handle event binding
jQuery.fn[ name ] = function( data, fn ) {
    if ( fn == null ) {
        fn = data;
        data = null;
    }

    return arguments.length > 0 ?
        this.on( name, null, data, fn ) :
        this.trigger( name );
};
...
});

是的,事实上,有一个更好的方法,使用.on() :看看这个

As suggested you can use .on() . 如建议你可以使用.on() As far as your question is concerned, .on() and also .delegate() for that matter has better performance than binding events to the target directly. 就你的问题而言, .on().delegate()对于这个问题具有比直接将事件绑定到目标更好的性能。 That is because these binders listens to an element which is higher in the DOM tree and then checks the target, as the pool of your targets increases .on() and .delegate() will surely give you performance benefit. 这是因为这些绑定器监听DOM树中较高的元素然后检查目标,因为目标池增加.on().delegate()肯定会给你带来性能优势。

And in general, they will always be more efficient. 一般来说,它们总是会更有效率。

I'm pretty sure that if all the items of class "toggles" are present when that binding is made, that delegate() would be less efficient. 我非常确定如果在进行绑定时所有类“切换”都存在,那么该委托()效率会降低。 It binds to everything of class "toggles", and then, when one of those elements is clicked, checks to see if the actual DOM object being clicked is an input. 它绑定到类“切换”的所有内容,然后,当单击其中一个元素时,检查所查看的实际DOM对象是否为输入。

click(), on the other hand, is a shortcut for bind(), which binds directly to everything of class "toggle" and always triggers when that DOM item is clicked. 另一方面,click()是bind()的快捷方式,它直接绑定到类“toggle”的所有内容,并且在单击该DOM项时始终触发。

The only reason to use delegate() would be if inputs are added inside items of class "toggle" after the binding is made. 使用delegate()的唯一原因是,在绑定完成后,如果在“toggle”类的项中添加了输入。

This also applies to using different overloads of on(), which replaces delegate() and bind() (and by replacing bind, also replaces click()). 这也适用于使用on()的不同重载,它取代了delegate()和bind()(并且通过替换bind,也替换了click())。

Yes it is. 是的。 Using .delegate will reduce the number of event handlers. 使用.delegate将减少事件处理程序的数量。

Suppose you have a table in your page and you want to attach click handler to each <td> . 假设您的页面中有一个表,并且您希望将click处理程序附加到每个<td> Instead of attaching event handler to individual <td> s attach it to the table: 而不是将事件处理程序附加到单个<td>将其附加到表:

$("#myTable").delegate("click", "td", function() {
    //do some thing on click of td
});

When you click on the <td> the event will bubble up to the table. 单击<td> ,事件将冒泡到表中。 Here by using delegate we can reduced the number of event handlers, which improves the performance. 通过使用委托,我们可以减少事件处理程序的数量,从而提高性能。

delegate is deprecated in latest version of jQuery use .on instead it works like delegate. 委托弃用的jQuery的最新版本使用.on ,而不是它的工作原理是委托。

There's another addition that you should be aware of concerning the use of on (delegate in older versions) vs click: 关于on(旧版本中的委托)与点击的使用,你应该知道另外一个补充:

if you do this : 如果你这样做:

 $("a").click(function() {  
  alert("Click on link detected");  
  $(this).after("<a href='#'>New link</a>");  
});

your new link will not be attached to the "a" click handler even though it should because it is a link. 您的新链接不会附加到“a”点击处理程序,即使它应该是因为它是一个链接。

however if you use on : 但是如果你使用:

$("a").on("click", function(event){  
   $(this).after("<a href='#'>Nouveau lien</a>");  
});  

with this the new link responds to the click event with the click handler. 使用此链接,新链接将使用单击处理程序响应click事件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CSS:明智的选择,最好使用calc或absolute - CSS: Performance wise, better to use calc or position absolute 这些ArrayToMap函数用法中的哪一种在性能方面更好? - Which one of these ArrayToMap functions usages is better performance-wise? 在性能方面,在算法复杂度方面,以下两种用于将字符串的第一个字母首字母化的JS功能哪个更好,为什么? - Performance-wise,algorithmic complexity-wise which of the following two JS fucntion for capitializing the first letter of a string is better and why? 正确使用以下语句性能的正确方法是哪种? - Which is right way to use below statement performance wise? jQuery on()方法-哪种使用方式对性能更好? - jQuery on() method - which way to use is better for performance? 在这种情况下,缓存jQuery对象是否会带来更好的性能结果? - Does caching a jQuery object in this case give better results performance-wise? if + override变量vs.if + else条件。 性能上哪个更好? - if+override variable vs. if+else condition. Which is better, performance wise? AngularJS - 哪个范围的性能更好? Object.key还是一些变量? - AngularJS - Which scope is better performance wise? Object.key or Some Variable? 有没有比 crypto.randomBytes 更好的方法来在性能方面生成唯一的 id? - Is there a better way than crypto.randomBytes to generate unique ids in performance-wise? 在这种特定情况下,if-else 是否比 try-catch 更好,性能方面? 哪种方式是最佳实践? - Is if-else better than try-catch in this particular scenario, performance wise? Which way is the best practice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM