Better than :last
is :last-child
: http://api.jquery.com/last-child-selector/
$("div:last-child").addClass("class");
// this gets both the last div and the last div of another div
The difference being twofold:
:last-child
can match more than one element; e.g., one per parent element, so that if your selector is complex at all, it will match everything you mean to match. (See page for example.)
:last-child
coincides with CSS2 terminology, so you can style just like you select in jQuery.
Examples
From what you've said, you could select what you want like this:
$("body > div:first-child > div:last-child").addClass("last")
.children("div").addClass("a")
.children("div").addClass("b")
.children("div").addClass("c");
You don't actually need the "div" inside .children()
, but I'm just demonstrating that you can filter the results--only DIV
s that are children of each respective level would get the class.