CSS
The other reliable way to replicate this currently is with the adjacent sibling selector:
.parent,
.parent + .parent + .parent,
.parent + .parent + .parent + .parent + .parent /* etc */
{ background-color: green; }
.parent + .parent,
.parent + .parent + .parent + .parent /* etc */
{ background-color: red; }
You have three options:
- Use the
not()
selector. This will keep your highlighting going indefinitely, but it will occasionally flip the order that it highlights in. Use this option if your list could have huge groupings of the elements you want to highlight.
- Use the
+
(adjacent sibling) selector. This option will not keep highlighting indefinitely, but it guarantees that the order will never be flipped. Use this option if your list will have smaller groupings of highlighted elements together.
- Use the
~
(any sibling) selector. I would not recommend this as the list will fail to highlight properly based on total list length rather than total matching siblings. This will always fail before the other two options, and more noticeably.
Fiddle here: http://jsfiddle.net/corymcd/kVcZJ/
Feel free to copy the HTML from this and paste it into the ones that the other people demonstrated their methods with.
jQuery
As stated before, using something like jQuery would easily allow you to either assign your elements even/odd classes or simply change the inline style.
// Find all objects to highlight, remove all of the highlighting classes,
// and then re-highlight.
$(".parent").removeClass('odd even').each(function(index) {
var objPrev = $(this).prev('.parent');
if( objPrev.hasClass('odd') ) {
$(this).addClass('even');
} else {
$(this).addClass('odd');
}
});
Fiddle here: http://jsfiddle.net/corymcd/kAPvX