jQuery-将多个选择器缓存到一个变量? [英] jQuery - caching multiple selectors to one variable?

查看:91
本文介绍了jQuery-将多个选择器缓存到一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对jQuery缓存进行一些说明.据我了解,

I need a bit of clarification on jQuery caching. To my understanding, this:

var $element = $('#element');

如果我们多次引用此元素(给我,我的意思是说要更改它,或者监听它的事件),就会给我们带来好处.这是正确的吗?

gives us benefit if we will refer to this element more than once (by refer I mean change it, or listen for an event on it.). Is this correct?

如果正确,以下代码是否有意义,为什么?

If it is correct, does the following code make any sense, or not, and why?

var $button = $('#submit, #save, #reset');

我看到的是jQuery允许我执行此操作(因此将相同的漂亮主题应用于所有3个按钮),但是我找不到在任何地方都能以任何方式提高性能的文档.

What I see is that jQuery allows me to do this (hence applying the same nice theme to all 3 buttons), but I cannot find docs anywhere does this help performance in any way.

推荐答案

是的,但前提是您必须将这些按钮设置为不止一次.这不可能.

Yes it makes sense, but only if you have to do something more than once with those buttons as a set. It's not probable.

我建议您在真正做到高频率(例如动画)之前不要对jquery优化进行过多的工作.

I suggest you don't make too much effort with jquery optimization until you really do things at high frequency (like animations).

这篇关于jQuery-将多个选择器缓存到一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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