(^4)
(^5)
(^6)
(^7)
(^8)
(^9)
- Sub link 2.1
11
(^12) - Sub link 2.2
13
(^14)
(^10)
(^15)
16
Imagine we wanted to color every third-generation
orange. Simple:
1 $('#nav li').each(function() {
(^2) if ($(this).parents('#nav li').length == 2)
(^3) $(this).css('color', '#f90');
4 });
This translates like so: for every
loop), whether it’s a direct child or not, see how many
ancestors are above it within #nav. If the number is two, then this
must be on level three.
closest(selector)
This is a bit of a well-kept secret, but very useful. It works like parents(),
except that it returns only one parent/ancestor. In my experience, you’ll
normally want to check for the existence of one particular element in an