Stripe class for div
Asked Answered
F

3

5

I have a long list of multiple div... let's say 20 div

All wrap in another one..

<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 class "grey" in one out of two div and make it zebra ! jquery please !

Fireplace answered 13/12, 2010 at 15:20 Comment(5)
excuse.. some corrections done (id/class)Fireplace
Actually your old code was fine. Your new one is invalid because you have duplicate IDs.Vallievalliere
In fact, you don't need the xyz class at all. If all elements inside another element have the same class you might as well remove it and select them by #main > div.Philomena
not ALL MY CODE... work with OTHER code... have to life and get thing done...Fireplace
Are you saying this is code you can't change, but it does have multiple id's? That's a tough job to be in, I've been there and it's annoying. Still, my answer should work :)Philomena
P
6

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

Do mind that 'grey' is not a semantic classname. Better call id 'odd' or 'zebra' or something. If you'd make up your mind and change the odd color to blue your classname would be real strange :P

Philomena answered 13/12, 2010 at 15:22 Comment(0)
F
2

jQuery makes it just about as easy as it can be:

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

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

Fauch answered 13/12, 2010 at 15:21 Comment(0)
V
1

If you don't care about older versions of IE, you can do this using CSS alone:

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

.xyz:nth-child(even) {
  background-color: ...;
}
Vallievalliere answered 13/12, 2010 at 15:24 Comment(2)
i care !... must be ie6 friendly !Fireplace
unfortunately even IE8 doesn't support this.. big bummer.Philomena

© 2022 - 2024 — McMap. All rights reserved.