jquery tablesorter plugin - retain alternative row colors

前端 未结 6 1533
没有蜡笔的小新
没有蜡笔的小新 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:31

    Based on Anthony's answer, but rephrased as a one-liner (mostly):

    function fixStripes() {
        $('table tr').removeClass('odd even')
            .filter(':even').addClass('even').end()
            .filter(':odd').addClass('odd');
    }
    
    $("table").bind("sort", fixStripes);
    

    JQuery calls can be "chained" as above, using operations like filter() to limit the selected elements, and .end() to "reset" to the last selection. Put another way, each .end() "undoes" the previous .filter(). The final .end() is left off, since there's nothing to do after that.

提交回复
热议问题