【问题标题】:Add class to the tr alternate将类添加到 tr 备用
【发布时间】:2013-03-01 13:03:19
【问题描述】:

我正在使用网格控件,其中表格有一个隐藏的 tr,单击该按钮即可打开。所以我尝试了以下函数将类添加到奇数行。

<script type="text/javascript">
// Activate TableStyling
jQuery(document).ready(function () {
    $('table.management').each(function () {
        $(this).children('tbody').children(':odd').addClass('grey');
    });

});

但问题是它也计算隐藏的 TR .. 所以我需要不计算隐藏 TR 的函数并将类添加到查看的 Tr

【问题讨论】:

    标签: jquery html-table tablerow


    【解决方案1】:

    你可以这样做:

    $(this).children('tbody').children(':visible:odd').addClass('grey');
    

    另外,如果你隐藏的tr 有一些特殊的类或其他东西,你可以这样做:

    $(this).children('tbody').children(':not(.<your hidden class>):odd')
        .addClass('grey');
    

    【讨论】:

      【解决方案2】:

      试试

      $(this).children('tbody').children(':visible:odd').addClass('grey');
      

      http://jsfiddle.net/K3vCD/

      【讨论】:

      • 请在投票后发表评论!
      【解决方案3】:

      您可以尝试将:visible 伪选择器与:odd 混合使用。

      现场示例:http://jsfiddle.net/SAdEE/

      【讨论】:

        【解决方案4】:

        尝试添加:visible 选择器。

        $(this).children('tbody').children(':visible:odd').addClass('grey');
        

        【讨论】:

          【解决方案5】:

          大多数(如果不是全部)答案都假定您对“隐藏”的定义与 jQuerys 相同。在其他情况下,“隐藏”可能类似于高度:0px、带有重叠元素的模糊 z 索引等。在这些非常罕见的情况下,可以使用过滤器方法与您自己的逻辑一起对被视为“隐藏”的内容进行分类。

          $(this).children('tbody').filter(function(){
             return this.isNotAHiddenTR(); // your custom logic.
             // return $(this).is(':visible'); // the most likely solution
          }).children(':odd').addClass('grey');
          

          此外,:visible 选择器返回具有visibility: hidden 的元素。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-12-14
            • 2011-06-04
            • 1970-01-01
            • 1970-01-01
            • 2015-11-05
            • 1970-01-01
            • 1970-01-01
            • 2018-05-01
            相关资源
            最近更新 更多