先看看效果:
让表格相邻行的颜色不同

如何让表格相邻行的颜色不同呢?

如何让表格的行的颜色间隔不同呢?

表格的行间隔变色

有如下种方式

方式一:使用纯css

Css代码  让表格相邻行的颜色不同
  1. table.dictionaryList tr:nth-child(2n+3){  
  2.     background-color:#c0e0f7;  
  3. }  
  4. table.dictionaryList tr:nth-child(2n+2){  
  5.     background-color:#defcfe;  
  6. }  

 说明:n从零开始:0,1,2,3....

没有包含表格的第一行,因为第一行是标题.

 

方式二:使用JavaScript脚本实现

Js代码  让表格相邻行的颜色不同
  1. $(function(){  
  2.         $('div.queryResultDiv table.productList tr:odd').addClass('odd');  
  3.         $('div.queryResultDiv table.productList tr:even').filter(':gt(0)').addClass('even');   
  4.     })  

 说明:odd和even均是css类.

 

方式三:使用javascript脚本

Js代码  让表格相邻行的颜色不同
  1. function altRows(id){  
  2.               if(document.getElementsByTagName){  
  3.   
  4.                   var table = document.getElementById(id);  
  5.                   var rows = table.getElementsByTagName("tr");  
  6.   
  7.                   for(i = 0; i < rows.length; i++){  
  8.                       if(i % 2 == 0){  
  9.                           rows[i].className = "evenrowcolor";  
  10.                       }else{  
  11.                           rows[i].className = "oddrowcolor";  
  12.                       }  
  13.                   }  
  14.       }  
  15.   }  

 说明:参数id是表格的id属性值.

 

推荐方式一,使用纯css

相关文章:

  • 2021-06-17
  • 2022-12-23
  • 2021-10-19
  • 2022-01-30
  • 2021-11-13
  • 2021-10-14
猜你喜欢
  • 2021-05-29
  • 2022-12-23
  • 2021-10-01
  • 2021-09-27
  • 2022-12-23
  • 2022-12-23
  • 2022-03-04
相关资源
相似解决方案