首页 文章

使用CSS替代表格行颜色?

提问于
浏览
417

我正在使用具有交替行颜色的表格 .

tr.d0 td {
  background-color: #CC9999;
  color: black;
}
tr.d1 td {
  background-color: #9999CC;
  color: black;
}
<table>
  <tr class="d0">
    <td>One</td>
    <td>one</td>
  </tr>
  <tr class="d1">
    <td>Two</td>
    <td>two</td>
  </tr>
</table>

这里我使用的是 tr 类,但我只想用于 table . 当我使用表格比这个适用于 tr 替代 .

我可以使用CSS编写这样的HTML吗?

<table class="alternate_color">
    <tr><td>One</td><td>one</td></tr>
    <tr><td>Two</td><td>two</td></tr>
    </table>

如何使用CSS使行具有“斑马条纹”?

9 回答

  • 31

    您可以使用nth-child(奇数/偶数)选择器,但并非所有浏览器(ie 6-8, ff v3.0)都支持这些规则,因此为什么大多数解决方案都会回退到某种形式的javascript / jquery解决方案,以便将这些类添加到这些非兼容浏览器的行中以获取虎条纹效果 .

  • 125

    你有:nth-child()伪类:

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

    :nth-child() 的早期,browser support有点穷 . 这就是为什么设置 class="odd" 成为一种常见的技术 . 在2013年底我'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 .

  • 12

    我可以使用css写这样的html吗?

    是的你可以但是你必须使用 :nth-child() 伪选择器(虽然支持有限):

    table.alternate_color tr:nth-child(odd) td{
       /* styles here */
    }
    table.alternate_color tr:nth-child(even) td{
       /* styles here */
    }
    
  • 9
    $(document).ready(function()
    {
      $("tr:odd").css({
        "background-color":"#000",
        "color":"#fff"});
    });
    
    tbody td{
      padding: 30px;
    }
    
    tbody tr:nth-child(odd){
      background-color: #4C8BF5;
      color: #fff;
    }
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table border="1">
    <tbody>
    <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    </tr>
    <tr>
    <td>5</td>
    <td>6</td>
    <td>7</td>
    <td>8</td>
    </tr>
    <tr>
    <td>9</td>
    <td>10</td>
    <td>11</td>
    <td>13</td>
    </tr>
    </tbody>
    </table>
    

    有一个CSS选择器,实际上是一个伪选择器,称为nth-child . 在纯CSS中,您可以执行以下操作:

    tr:nth-child(even) {
        background-color: #000000;
    }
    

    Note: IE 8中不支持 .

    或者,如果你有jQuery:

    $(document).ready(function()
    {
      $("tr:even").css("background-color", "#000000");
    });
    
  • 3
    <script type="text/javascript">
    $(function(){
      $("table.alternate_color tr:even").addClass("d0");
       $("table.alternate_color tr:odd").addClass("d1");
    });
    </script>
    
  • 9

    有一种相当简单的方法可以在PHP中执行此操作,如果我理解您的查询,我假设您使用PHP编写代码并使用CSS和javascript来增强输出 .

    来自数据库的动态输出将带有for循环以迭代结果,然后将结果加载到表中 . 只需添加一个函数调用,如下所示:

    echo "<tr style=".getbgc($i).">";  //this calls the function based on the iteration of the for loop.
    

    然后将该函数添加到页面或库文件中:

    function getbgc($trcount)
    {
    
    $blue="\"background-color: #EEFAF6;\"";
    $green="\"background-color: #D4F7EB;\"";
    $odd=$trcount%2;
        if($odd==1){return $blue;}
        else{return $green;}
    

    }

    现在,这将在每个新生成的表行的颜色之间动态交替 .

    它比使用CSS无法在所有浏览器上运行容易得多 .

    希望这可以帮助 .

  • 724

    只需将以下内容添加到您的html代码中(使用 <head> )即可完成 .

    HTML:

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

    比jQuery示例更容易,更快捷 .

  • 8

    我们可以使用奇数和偶数CSS规则以及jQuery方法来替换行颜色

    Using CSS

    table tr:nth-child(odd) td{
               background:#ccc;
    }
    table tr:nth-child(even) td{
                background:#fff;
    }
    

    Using jQuery

    $(document).ready(function()
    {
      $("table tr:odd").css("background", "#ccc");
      $("table tr:even").css("background", "#fff");
    });
    
    table tr:nth-child(odd) td{
               background:#ccc;
    }
    table tr:nth-child(even) td{
                background:#fff;
    }
    
    <table>
      <tr>
        <td>One</td>
        <td>one</td>
       </tr>
      <tr>
        <td>Two</td>
        <td>two</td>
      </tr>
    </table>
    
  • 3

    大多数上述代码不适用于IE版本 . 适用于IE其他浏览器的解决方案就是这样 .

    <style type="text/css">
          tr:nth-child(2n) {
                 background-color: #FFEBCD;
            }
    </style>
    

相关问题