Jquery to achieve a better effect of the table selected row color

  • 2020-03-30 02:26:33
  • OfStack

Jquery table select line color (better)
 
<html> 
<head> 
<style type="text/css"> 
.table-tr-title{ 
height: 26px; 
font-size: 12px; 
text-align: center; 
} 
.table-tr-content{ 
height: 18px; 
background: #FFFFFF; 
text-align: center; 
font-size: 12px; 
} 
.normalEvenTD{ 
background: #DFD8D8; 
} 
.normalOddTD{ 
background: #FFFFFF; 
} 
.hoverTD{ 
background-color: #eafcd5; 
height: 18px; 
text-align: center; 
font-size: 12px; 
} 
.trSelected{ 
background-color: #51b2f6; 
height: 18px; 
text-align: center; 
font-size: 12px; 
} 
</style> 
</head> 
<body> 
<table width="99%" class="list" style="word-break: break-all" border="0" 
align="center" cellpadding="0" cellspacing="1" bgcolor="#c0de98"> 
<tr class="table-tr-title"> 
<td width="5%"> The title </td> 
<td width="5%"> The title </td> 
<td width="5%"> The title </td> 
<td width="5%"> The title </td> 
</tr> 
<tr class="table-tr-content"> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
</tr> 
<tr class="table-tr-content"> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
</tr> 
<tr class="table-tr-content"> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
</tr> 
<tr class="table-tr-content"> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
</tr> 
<tr class="table-tr-content"> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
</tr> 
<tr class="table-tr-content"> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
</tr> 
<tr class="table-tr-content"> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
</tr> 
<tr class="table-tr-content"> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
</tr> 
<tr class="table-tr-content"> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
</tr> 
<tr class="table-tr-content"> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
</tr> 
<tr class="table-tr-content"> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
</tr> 
<tr class="table-tr-content"> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
<td width="5%"> data </td> 
</tr> 
</body> 
</html> 

Js code:
 
<script type="text/javascript" src="jquery-1.6.4.js"></script> 
<script> 
$(function(){ 
///////datagrid Select line color and mouse over line color /////////////// 
var dtSelector = ".list"; 
var tbList = $(dtSelector); 

tbList.each(function() { 
var self = this; 
$("tr:even:not(:first)", $(self)).addClass("normalEvenTD"); //An odd number of rows starting from the next line in the header line :(1,3,5...)
$("tr:odd", $(self)).addClass("normalOddTD"); //An even number of rows starting from the next row of the header row, number of rows :(2,4,6...)

//Mouse over the line changes color
$("tr:not(:first)", $(self)).hover( 
function () { $(this).addClass('hoverTD');$(this).removeClass('table-td-content'); }, 
function () { $(this).removeClass('hoverTD');$(this).addClass('table-td-content'); } 
); 

//Select line color
$("tr", $(self)).click(function (){ 
var trThis = this; 
$(self).find(".trSelected").removeClass('trSelected'); 
if ($(trThis).get(0) == $("tr:first", $(self)).get(0)){ 
return; 
} 
$(trThis).addClass('trSelected'); 
}); 
}); 
}); 
</script> 

Effect display:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201403/201403251800464.gif? 20142251818 ">

Related articles: