문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top