You'll need to employ some strategic use of :not()
and adjacent sibling selectors:
$('tr.odd:first-child, tr.even:first-child, tr:not(.odd) + tr.odd, tr:not(.even) + tr.even')
The tr:not(...) + tr...
bits select any tr
element that's directly preceded by a tr
that does not have the same class name. You can't do this dynamically (e.g. with some sort of wildcard), but it's entirely possible if you specify each class name separately. This will ensure you only select the first element in each contiguous group.
The reason the :first-child
pseudo is required is simply because +
won't match the first child, since it implies a relationship between an element and its preceding sibling. Note that :first-child
accounts for the very first tr
only. You could replace tr.odd:first-child, tr.even:first-child
with simply tr:first-child
if every tr
element will have exactly one of either class name, but I specify them anyway for clarity's sake.
Last but not least, this doubles as a valid CSS selector, so you can use it in a stylesheet as well if you like.