The jQuery Fundamentals page links to the CSS2 spec on CSS Selectors, but there are some changes in the CSS3 spec. Memorizing the spec details isn't the most critical thing for me right now, and I have a pretty good grasp of combinators and pseudo-elements, IDs and classes.
The trouble with W3C specs, of course, is that user agent (UA, aka browsers) implementations may vary. Pseudo elements: I should try out
:first-letter
for drop-caps (though it wont help with my image drop-caps).Q: So what exactly is a jQuery object? What does one do with it once it's created? Where is it stored and what is its lifespan? Backing up slightly, what does a regular old JS object look like?
A: As usual, I'm complicating things. Your generic JS object looks about like the code below, using literal notation to make the comparison easier. "All objects in JavaScript are descended from
Object
; all objects inherit methods and properties from Object.prototype
, although they may be overridden" (MDN reference) "Objects are a collection of properties, and properties are associations between a name and a value"(more MDN). If a property is a function, then it is a method for that object. Pretty much everything in JS is actually an object. (see also: prototypes and classes)
var Bunny = {
ears : "long",
tail : "puffy",
hop : function (height) {
console.log("Hopped "+ height);
}};
The jQuery object is a JS object in that it stores elements and has methods, but it is an array-like object that just lists the DOM elements from the query. An array-like object has key:value pairs, but the key in this case seems to just be the index.
var a = $('div');
console.log(a);
/*returns from my blog homepage, more or less:
{
0: div#navbar.navbar section,
1: div#Navbar1.widget Navbar,
2: div#navbar-iframe-container,
3: div.body-fauxcolumns...}
*/
The jQuery object can be stored as a variable, or just called once and used, otherwise it disappears after use (?). I'd like to better understand memory and troubleshooting leaks (StackOverflow discussion). Basically, it looks like as long as there's a reference to an object, that object has memory allocated to it.Helpful tidbits:
$()
is a function. Functions in JS are objects and have methods and properties.$()
always returns an object, and objects are always truthy.$( 'li' ).eq( 0 ).is( '.special' );
to find whether the selection using.eq()
to select a specific single{ element matches the parameter in the.is()
method (selector, DOM element, jQuery object, function); returns boolean.- Use getters and setters (methods) to do things with jQuery objects. Iteration over elements can be explicit (
.each();
)or implicit (.html();
), and you can chain methods together on a single selection. - Then we also have filtering,
.not(); .filter(); .has();
on selections, which are more or less "without," "within previous selection," and "contains," respectively. ...many more methods here in the top hit on Google for "jQuery cheatsheet" (includes CSS selectors!). I'm a visual person. - Plus, find other items/make other selections relative to a previous selection with things like
.children();
,.next();
,.parent();
etc and add to existing selection with, by george,.add();
- More on how to use jQuery in the context of your basic HTML page here from the jQuery folks.