05.01.2013 Views

hide - Understanding jQuery

hide - Understanding jQuery

hide - Understanding jQuery

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

To traverse the DOM and make sure that we are selecting the right thing, we can use the<br />

SPACE ( ) operator. The space operator lets DOM know exactly which branch we want to traverse:<br />

// Hide all DIVs inside BODY, but not BODY itself<br />

<strong>jQuery</strong>(‘body div’).<strong>hide</strong>();<br />

// Hide all DIVs inside first DIV child of BODY, but not DIV child itself<br />

<strong>jQuery</strong>(‘body div div’).<strong>hide</strong>();<br />

// Hide all DIVs inside first DIV child of BODY whose id is #secret<br />

<strong>jQuery</strong>(‘body div#secret div’).<strong>hide</strong>();<br />

Note that none of the elements before the last space are actually selected. Only the last one is.<br />

The values before the last space simply indicate the DOM path to traverse, but the path itself is<br />

not selected, only the last element in the path.<br />

CSS Selector Reference<br />

The following diagrams visually explain how many <strong>jQuery</strong> CSS selectors work. Print it out,<br />

you may find it as a useful reference to keep on your desk. Green indicates selected result.<br />

In the first two examples we select arbitrary elements, one with id “clockwork” and another<br />

with id “orange”. We then use the prev() and next() functions respectively to traverse back or<br />

forward within the parent element. Function prev() steps one element back within the same<br />

hierarchy. Function next() steps one element forward. The third example doesn’t select anything<br />

at all. Why not? This is because there are no previous elements before the element with<br />

id “orange”. The prev() function will not select the parent of current hierarchy.<br />

78

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!