【问题标题】:Selecting the same item with jQuery multiple times vs using a variable使用 jQuery 多次选择同一个项目与使用变量
【发布时间】:2010-08-20 21:32:15
【问题描述】:

从理论上讲,以下哪项在性能和/或内存方面会更好?换句话说,jQuery 是否必须多次选择同一个项目,还是知道它过去选择了哪些项目?

我故意让动画没有参数,这是一个纯粹的理论问题。

$("#someelement").animate();
$("#someelement").animate();
$("#someelement").animate();
$("#someelement").animate();
$("#someelement").animate();
$("#someelement").animate();
$("#someelement").animate();
$("#someelement").animate();
$("#someelement").animate();
$("#someelement").animate();

var element = $("#someelement");
element.animate();
element.animate();
element.animate();
element.animate();
element.animate();
element.animate();
element.animate();
element.animate();
element.animate();
element.animate();

【问题讨论】:

    标签: javascript jquery theory


    【解决方案1】:

    绝对是选项 2。使用选项 1,jQuery 必须在每个动画之前找到 DOM 中的元素。

    使用选项 2,元素被“缓存”在变量中,无需再次遍历 DOM。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      • 2010-12-24
      • 1970-01-01
      • 2012-01-03
      相关资源
      最近更新 更多