jquery tablesorter plugin - retain alternative row colors

前端 未结 6 1534
没有蜡笔的小新
没有蜡笔的小新 2021-01-01 20:36

I took an html table that I am applying alternative row colors to, and I added jquery table sorter on it so users can sort the table.

The issue is that the alternati

6条回答
  •  抹茶落季
    2021-01-01 21:17

    How about:

    function fixStripes() {
         var i = 0;
         var rowclass;
         $("table tr").each(function() {
              $(this).removeClass("odd even");
              rowclass = (i%2 == 1) ? "odd" : "even";
              $(this).addClass(rowClass);
          });
    }
    
    $("table").bind("sort", fixStripes);
    

    Oh, and if you want a really simple fix, you could hold your breath for this CSS pseudo-class to get picked up by all the major browsers:

    table tr:nth-child(n+1) {
        color: #ccc;
    }
    

    But my guess is based on how FF and company handle CSS for dynamic HTML, it would set your stripes onload, but not apply the CSS after you sort. But I'd like to know for sure.

提交回复
热议问题