這篇文章主要介紹了jquery table選中行顏色實現代碼,需要的朋友可以參考下
jquery table選中行顏色(效果更好) 代碼如下: <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%">標題</td> <td width="5%">標題</td> <td width="5%">標題</td> <td width="5%">標題</td> </tr> <tr class="table-tr-content"> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> </tr> <tr class="table-tr-content"> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> </tr> <tr class="table-tr-content"> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> </tr> <tr class="table-tr-content"> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> </tr> <tr class="table-tr-content"> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> </tr> <tr class="table-tr-content"> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> </tr> <tr class="table-tr-content"> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> </tr> <tr class="table-tr-content"> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> </tr> <tr class="table-tr-content"> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> </tr> <tr class="table-tr-content"> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> </tr> <tr class="table-tr-content"> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> </tr> <tr class="table-tr-content"> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> <td width="5%">數據</td> </tr> </body> </html> js代碼: 代碼如下: <script type="text/javascript" src="jquery-1.6.4.js"></script> <script> $(function(){ ///////datagrid選中行變色與鼠標經過行變色/////////////// var dtSelector = ".list"; var tbList = $(dtSelector); tbList.each(function() { var self = this; $("tr:even:not(:first)", $(self)).addClass("normalEvenTD"); // 從標頭行下一行開始的奇數行,行數:(1,3,5...) $("tr:odd", $(self)).addClass("normalOddTD"); // 從標頭行下一行開始的偶數行,行數:(2,4,6...) // 鼠標經過的行變色 $("tr:not(:first)", $(self)).hover( function () { $(this).addClass('hoverTD');$(this).removeClass('table-td-content'); }, function () { $(this).removeClass('hoverTD');$(this).addClass('table-td-content'); } ); // 選擇行變色 $("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> 效果顯示: