cycling jQuery_Succinctly | Page 31

Imagine the possibilities ahead of you with the ability to enforce iteration anytime you please.
Notes: jQuery also provides a $. each function, not to be confused with the $(). each method, which is used specifically to iterate over a jQuery wrapper set. The $. each method can actually be used to iterate over any old JavaScript array or object. It is essentailly a substitue for native JavaScript loops.
Elements in jQuery wrapper set returned in document order
The selector engine will return results in document order as opposed to the order in which the selectors were passed in. The wrapper set will be populated with the selected elements based on the order each element appears in the document, from top to bottom.
Sample: sample19. html
<! DOCTYPE html > < html lang =" en "> < body > < h1 > h1 </ h1 > < h2 > h2 </ h2 > < h3 > h3 </ h3 > < script src =" http:// ajax. googleapis. com / ajax / libs / jquery / 1.7.2 / jquery. min. js "></ script >
< script >( function($) { // We pass in h3 first, but h1 appears earlier in // the document, so it is first in the wrapper set. alert($(' h3, h2, h1 '). get( 0). nodeName); // Alerts " H1 ".
})( jQuery);
</ script > </ body > </ html >
Determining context used by the jQuery function
The default context used by the jQuery function when selecting DOM elements is the document element( e. g. $(' a ', document)). This means that if you do not provide the jQuery function( e. g. jQuery()) with a second parameter to be used as the context for the DOM query, the default context used is the document element, more commonly known as < body >.
31