Divs with alternating colors

Asked

Viewed 88 times

2

I have an ajax that shows the results to me in a JSON, I need the colors of the <div> alternem: in the first gray color, in the second white, in the third gray, in the fourth white and so on. How do I do this? I can put class 1 in the even div and class 2 in the odd div, I just need to know how to do.

Follow my code with html example:

<div class="historico_registroA">
    <span class="historico_registro_data">12/10/2016 às 11:00</span>
</div>
<div class="historico_registroB">
    <span class="historico_registro_data">22/11/2016 às 15:30</span>
</div>
<div class="historico_registroA">
    <span class="historico_registro_data">07/12/2016 às 18:00</span>
</div>
<div class="historico_registroB">
    <span class="historico_registro_data">09/12/2016 às 15:00</span>
</div>
  • http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_dom_addclass

1 answer

5


In css I think it would be easier, you’re trying to make the famous zebra list right? (correct me if I’m wrong).

suaClasse:nth-child(even) { background: #CCC; }
suaClasse:nth-child(odd) { background: #FFF; }
  • that’s right, helped me a lot, vlw!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.