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.

Whose Children Are These?<br />

If we are to avoid using IDs completely, except when we need to assign them to a context, how<br />

do we navigate the DOM?<br />

We do so by using parents and children. But which elements are children of which parent<br />

elements? How does the DOM determine that? Just like in the real world, children belong to<br />

parents. But those parents can also be children of their parents, and so on. It’s just a matter of<br />

perspective. Let’s take a look at this diagram that explains relationships between parents and<br />

children:<br />

If each box on this diagram represents an HTML element, then the first child of the body tag is<br />

also the parent of all green children.<br />

The second child of the body tag is also the parent of all yellow children.<br />

Each individual yellow “child” is also an HTML element. Then black children 1, 2 and 3 are the<br />

children of one of the yellow child whose parent is the second child of body tag. Trivial stuff<br />

when you have a visual guide.<br />

Using :nth-child Selector<br />

Instead of using IDs for everything we will use the :nth-child selector that works on tag names<br />

Let’s assume that we have a DIV and that we have a few other elements stored inside it; in particular<br />

a DIV element, a SPAN element followed by 3 more DIV elements:<br />

89

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

Saved successfully!

Ooh no, something went wrong!