Alternate table row color using CSS?

后端 未结 9 1765
囚心锁ツ
囚心锁ツ 2020-11-22 09:03

I am using a table with alternate row color with this.

相关标签:
9条回答
  • 2020-11-22 09:41

    You have the :nth-child() pseudo-class:

    table tr:nth-child(odd) td{
        ...
    }
    table tr:nth-child(even) td{
        ...
    }
    

    In the early days of :nth-child() its browser support was kind of poor. That's why setting class="odd" became such a common technique. In late 2013 I'm glad to say that IE6 and IE7 are finally dead (or sick enough to stop caring) but IE8 is still around — thankfully, it's the only exception.

    0 讨论(0)
  • 2020-11-22 09:44
    <script type="text/javascript">
    $(function(){
      $("table.alternate_color tr:even").addClass("d0");
       $("table.alternate_color tr:odd").addClass("d1");
    });
    </script>
    
    0 讨论(0)
  • 2020-11-22 09:54

    Just add the following to your html code (withing the <head>) and you are done.

    HTML:

    <style>
          tr:nth-of-type(odd) {
          background-color:#ccc;
        }
    </style>
    

    Easier and faster than jQuery examples.

    0 讨论(0)
提交回复
热议问题