Web Development with jQuery®

(Elliott) #1

(^54) ❘ CHAPTER 2 SELECTING AND FILTERING
the selection to the fi rst element of that row by calling eq(0). Zero, this time, represents the
fi rst child element.
var nextWeek = today.parent('tr').next('tr');
if (nextWeek.length)
{
tomorrow = nextWeek.children('td').eq(0);
}
}
When you have determined which element, if any, represents tomorrow, you check to see if you have
a selection, and if you do, you add the class name calendarTomorrow to that element.
if (tomorrow.length)
{
tomorrow.addClass('calendarTommorow');
}
The next exercise is to identify all days after today, which will represent later this week. That is
done by calling nextAll() on the selection representing today, which brings back a selection of
elements, all of which are siblings to the element representing today, but all occur after today.
var laterThisWeek = today.nextAll('td');
if (laterThisWeek.length)
{
If there is a selection of elements, those elements all receive the class name
calendarLaterThisWeek.
laterThisWeek.addClass('calendarLaterThisWeek');
}
Then, you do the same thing to identify elements that will qualify for the phrase earlier this
week. To identify those elements, you call prevAll() on today to select all elements preceding
the element representing today.
var earlierThisWeek = today.prevAll('td');
if (earlierThisWeek.length)
{
If there are elements assigned to the variable earlierThisWeek, those elements each receive
the class name calendarEarlierThisWeek.
earlierThisWeek.addClass('calendarEarlierThisWeek');
}
Finally, you identify all sibling elements of the element representing today using jQuery’s
siblings() method, and those elements are all given the class name calendarThisWeek.
today.siblings('td')
http://www.it-ebooks.info

Free download pdf