【问题标题】:How to apply css to table rows如何将css应用于表格行
【发布时间】:2013-01-28 16:17:25
【问题描述】:

我需要为我的 html 表格的交替行应用背景颜色。

我当前的代码:

.licList
    {
        width: 100%;
        border: 1px solid #f7f7f7;
        text-align: center;
        border-collapse: collapse;
    }

    .licList th
    {
        font-weight: bold;
        background-color: #dbdbdb;
        border-bottom: 1px solid #f1f1f1;
        padding: 4px 5px;
    }

    .licList td
    {
        padding: 4px 5px;
    }

    .odd
    {
        background-color: #ffffff;
    }
    .odd td
    {
        border-bottom: 1px solid #cef;
    }

jquery 是

    $(document).ready(function () {
        $("licList:tr:odd").addClass("odd");
    });

我确定上面的 jquery 不正确我在单页中有多个表,所以我不能申请

$(document).ready(function(){
    $("tr:odd").addClass("odd");
});

所以我的问题是如何解决这个问题????

【问题讨论】:

    标签: c# jquery asp.net html


    【解决方案1】:

    jquery 不正确我在单页中有多个表,所以我不能 申请

    id/class 分配给您的表并访问该表下的行。假设你的表有 id tbl

    Live Demo

    <table id="tbl">
       <tr>
            <td>1</td>
            <td>1</td>
       </tr> 
       <tr>
            <td>1</td>
            <td>1</td>
       </tr> 
    </table>
    
    $(document).ready(function(){
        $("#tbl tr:odd").addClass("odd");
    });
    

    【讨论】:

    • ..... $(document).ready(function(){ $("tbl tr:odd").addClass("odd"); } );不工作
    【解决方案2】:

    请记住,jQuery 选择器就像 CSS 选择器一样,所以 licList:tr:odd 不是一个有效的选择器。你可以只用 CSS 来做到这一点:

    .licList:nth-child(odd) { ... }
    

    【讨论】:

      【解决方案3】:

      偶数行和奇数行都有一个 Jquery 选择器。您可以将其与表 id 一起使用,

      $(document).ready(function(){
          $("#table1 tr:odd").addClass("odd");
          $("#table1 tr:even").addClass("even");
      });
      

      【讨论】:

        【解决方案4】:

        CSS3 允许这样做,不要使用 javascript,因为你可以轻松做到这一点

        tr:nth-child(odd)    { background-color:#eee; }
        tr:nth-child(even)    { background-color:#fff; }
        

        DEMO

        【讨论】:

          【解决方案5】:

          如果您使用jQuery.each(),那么它将是table 的两个集合,您可以从那里过滤而不是tr 的大量集合

          $('table').each(function(){
              $(this).find('tr').filter(':odd').addClass("odd"); //first rows always the same
          });
          

          fiddle

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-11-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-01-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多