text te...">

Stripe class for div

I have a long list of several divs ... let 20 div

All wrapping in another ..

<div id="main">
 <div class="xyz"> text text </div>
 <div class="xyz"> text text </div>
 <div class="xyz"> text text </div>
 <div class="xyz"> text text </div>
 <div class="xyz"> text text </div>
.... etc
</div>

I like to add the gray class to one of the two divs and make it a zebra! jquery please!

+5
source share
3 answers

$('.xyz:odd').addClass('grey');

Keep in mind that gray is not a semantic class name. It's better to call the identifier "odd" or "zebra" or something else. If you decide and change the odd color to blue, your class name would be strange: P

+6
source

jQuery makes this about as simple as it can be:

$('#main>div.xyz:even').addClass('grey');

http://api.jquery.com/even-selector/

+2
source

IE, , CSS:

.xyz:nth-child(odd) {
  background-color: ...;
}

.xyz:nth-child(even) {
  background-color: ...;
}
+1

All Articles