05.01.2013 Views

hide - Understanding jQuery

hide - Understanding jQuery

hide - Understanding jQuery

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The :nth-child(2) will select the span element. Index 2, in this case is the literal 2nd child. Indexing,<br />

in this case, does not begin with 0.<br />

A Small Detail About Using :nth-child Selector<br />

The <strong>jQuery</strong>’s implementation of nth-child selector is somewhat peculiar when dealing with<br />

children of different types in the same parent. If we take the diagram from above as an example<br />

and create a selector such as this one:<br />

$(“body div div:nth-child(2)”)<br />

It will not select anything at all. The second DIV child (whatever that may mean in this case?)<br />

simply doesn’t exist. Because the second child is an element of type SPAN, not DIV. Logically,<br />

you would think this selector would choose the DIV after the SPAN element. But that would<br />

be wrong, because that DIV element can be rightfully selected as div:nth-child(3).<br />

Had all of the elements been of type DIV, then this selector would actualy work.<br />

You can see now that this selector prioritizes the notion of child, not what type that child is...<br />

but that it is a child. It’s just how it works when different types of elements happen to be children<br />

of the parent element. This is not a bug.<br />

90

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

Saved successfully!

Ooh no, something went wrong!