Binding event on document performance concerns

浪子不回头ぞ 提交于 2020-01-06 04:28:08

问题


Does binding an event on document have any performance concerns?

i.e:

$(document).on('mouseenter mouseleave', function(e){
    if (e.type === 'mouseenter'){
        $(this).find( //some element and do something...

Vs

$(".myElement").hover(function(){
    $(this).find( //some element and do something...

I think keeping a track via document will use more processing power then assigning the event only a limited DOM elements?


回答1:


You might need to implement a performance test to be sure about the actual difference, but I guess it should be a minimal impact, because binding events on the document will mean that you catch any element event once it bubbles to the top-most element in the document.

Anyways, skipping the "performance argument", your case looks better when you bind a handler on the nearest parent. It's not only about performance: it's more logical.



来源:https://stackoverflow.com/questions/23204156/binding-event-on-document-performance-concerns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!