Help to color-alternate rows in sortable table

血红的双手。 提交于 2019-12-04 05:34:35

问题


I'm using jQuery tablesorter plugin. I'm relatively new to jQuery and need help from jQuery ninjas.

My table has color-alternative rows. I used CSS nth-child() to alternate the rows.

table.tablesorter tbody tr:nth-child(odd) td {
    background-color:#FBFBFB;
}

It works fine in Chrome and Firefox but IE doesn't like it. IE doesn't support nth-child.

I tried to control color-alternating with JavaScript when page loads.

$(document).ready(function() {
  $('#packageTbl').tablesorter();
  $('table.tablesorter tbody tr:nth-child(odd) td').css('background-color', '#FBFBFB');
  $('table.tablesorter tbody tr:nth-child(even) td').css('background-color', '#DDD');
});

And it works fine when the page originally loads but when I click on a column to sort, my color-alternative rows do not alternate any more. I might have two white rows then three gray ones.

Can you suggest any solution that can help me.


回答1:


Tablesorter has a built in function to "zebra" stripe the rows, it also automatically updates the striping after sorting. Use it as follows:

CSS

.NormRow { background-color: #fbfbfb; }
.AltRow  { background-color: #ddd; }

Script

$('#packageTbl').tablesorter({
   widgets: ['zebra'],
   widgetZebra: {css: ["NormRow","AltRow"]} // css classes to apply to rows
});

Update: Actually, the default CSS the tablesorter uses is "odd" and "even" so change the css names to that if you wish.




回答2:


JavaScript:

$('.tablesorter tr:nth-child(odd)').addClass('odd');

CSS:

.odd {
    background-color:#FBFBFB;
}

Example can be found here.



来源:https://stackoverflow.com/questions/5783504/help-to-color-alternate-rows-in-sortable-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!