问题
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