缓存DOM元素 [英] Caching DOM elements

查看:215
本文介绍了缓存DOM元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有一个在事件处理程序中被多次访问但不是连续访问的代码中的DOM元素时,将它们存储在一个变量中是有益的吗?
示例 -

When I have a DOM elements that are accessed many times in event handlers but not consecutively in code, is it benefical to store them in a variable? Examples-


  1. 我有一个事件处理程序设置为 onresize - 在处理程序中,我通过ID获取元素。如我所知,这个事件在窗口调整大小时被多次触发,那么最好在全局变量中引用这些元素?

  1. I have an event handler set for onresize - in the handler I get elements by ID's. As I know, this event is triggered multiple times when window is resized, so would it be better to hold a reference to these elements in global variables?

我实现基于AJAX的搜索。我有一个输入文本元素,我已经为 onkeyup 事件设置了处理程序。在处理程序中,我需要通过ID获取一些元素来设置结果,还可以隐藏和显示(css)其他元素。相同的问题是 - 甚至给出一些性能优势来存储它们,还是没有意义?

I implement an AJAX based search. I have an input-text element and I've set handler for onkeyup event. In the handler I need to get some elements by ID to set the result, and also to hide and show (css) some other elements. Same question here- is that even gives some performance benefits to store them, or there's no point?


推荐答案

是的,它有利于存储在代码中或缓存它们,因为它可以节省处理能力。

Yes is it space beneficial to "store it in code" or cache them, as it saves processing power.

参见:内部有jQuery缓存元素?

这里是另一个例子:
http:// jquery- howto.blogspot.com/2008/12/caching-in-jquery.html

这篇关于缓存DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆