关于文档性能问题的绑定事件 [英] Binding event on document performance concerns
问题描述
在文档上绑定事件是否有性能方面的顾虑?
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.
这篇关于关于文档性能问题的绑定事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!