在文档上绑定事件是否有性能方面的顾虑?
Does binding an event on document have any performance concerns?
即:
$(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...我认为通过文档进行跟踪会使用更多的处理能力,然后仅将事件分配给有限的DOM元素?
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.
更多推荐
关于文档性能问题的绑定事件
发布评论