关于文档性能问题的绑定事件

编程入门 行业动态 更新时间:2024-10-28 04:30:58
本文介绍了关于文档性能问题的绑定事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在文档上绑定事件是否有性能方面的顾虑?

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.

更多推荐

关于文档性能问题的绑定事件

本文发布于:2023-10-18 03:30:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502953.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   性能   事件   文档

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!