I know it is faster to do the following:
var $header = $("#header"); $header.css({color:"#ff0000"}); $header.find("a").addClass("foo");
Instead:
$("#header").css({color:"#ff0000"}); $("#header a").addClass("foo");
Since jQuery does not need to search for elements in the DOM, since we have a direct link to them.
Say I have this:
var $header_elements = $("#header li"); var $footer_elements = $("#footer li");
And I use both separately for several jQuery manipulations. But then I need to do something on both. Using a selector, I would do the following:
$("#header li, #footer li").css({color:"#ff0000"});
But then the DOM needs to be analyzed again to find the appropriate elements. Is there a way to use my previously declared variables instead of a new selector? Something like the following (which does n't work, I know, this gives an idea of what I'm looking for):
$($header_elements + $footer_elements).css({color:"#ff0000"});
I think the selector returns some kind of array or object. What I'm looking for is a way to combine them. Does anyone know if this is possible and how to do it?
Thanks for your help!
jquery
Gabriel Aug 26 '10 at 15:22 2010-08-26 15:22
source share