Stripe class for div

2020-04-07 04:44发布

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 !

3条回答
我想做一个坏孩纸
2楼-- · 2020-04-07 04:49

$('.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

查看更多
不美不萌又怎样
3楼-- · 2020-04-07 04:55

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

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

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

查看更多
甜甜的少女心
4楼-- · 2020-04-07 05:04

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: ...;
}
查看更多
登录 后发表回答