admin管理员组文章数量:1243250
There are some times when I find myself repeating a selector several times. Should I be somehow storing a jquery object to a variable and then just using that one? As a quick example, what about the following?:
$('a.contactus').css('padding', '10px');
$('a.contactus').css('margin', '4px');
$('a.contactus').css('display', 'block');
Now I know this isn't a great example, since effectively you could just chain each css function. But suppose in between each of those was a conditional statement or something to stop you from chaining.
Can I store a jquery object in a variable? And if so, when should I / can I?
There are some times when I find myself repeating a selector several times. Should I be somehow storing a jquery object to a variable and then just using that one? As a quick example, what about the following?:
$('a.contactus').css('padding', '10px');
$('a.contactus').css('margin', '4px');
$('a.contactus').css('display', 'block');
Now I know this isn't a great example, since effectively you could just chain each css function. But suppose in between each of those was a conditional statement or something to stop you from chaining.
Can I store a jquery object in a variable? And if so, when should I / can I?
Share Improve this question edited Apr 3, 2014 at 18:35 Daniel Szalay 4,10112 gold badges60 silver badges105 bronze badges asked Oct 12, 2010 at 19:00 MatthewMatthew 15.7k28 gold badges91 silver badges124 bronze badges3 Answers
Reset to default 12When reusing it more than once (and you can't chain) storing it in a variable isn't a bad idea, the more often it's used or the more expensive the selector, the better idea storing it as a variable bees. For example the performance of $(this)
a few times is trivial, but the performance of $("[attr=val]")
is very poor and should absolutely be cached if reused. If in doubt, cache it as a variable.
Just another tip, in that example you can also pass an object to .css()
:
$('a.contactus').css({ padding: '10px', margin: '4px', display: 'block' });
Should I be somehow storing a jquery object to a variable and then just using that one?
You should for the sake of performance when possible. You can for example re-write your code like this:
var $el = $('a.contactus');
$el.css('padding', '10px');
$el.css('margin', '4px');
$el.css('display', 'block');
You can make it even shorter like this:
$el.css({
padding: '10px',
margin:'4px',
display: 'block'
});
Storing mon/repetitive selector in a variable is also useful when writing jquery plugins to store the $(this)
in a variable.
You can do this
var myvar = $('a.contactus');
myvar.css('padding', '10px').css('margin', '4px').css('display', 'block');
but for readability i do this
var myvar = $('a.contactus');
myvar.css('padding', '10px')
.css('margin', '4px')
.css('display', 'block');
basically every time you use $(someselector) you iterate through the dom. If you can you should store the element reference.
本文标签: javascriptJqueryShould I not repeat selectors (store in a variable)Stack Overflow
版权声明:本文标题:javascript - Jquery - Should I not repeat selectors (store in a variable)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740121009a2227983.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论