Home > Article > Web Front-end > jquery custom table style_jquery
The example in this article describes the jquery custom table style implementation code. Share it with everyone for your reference. The details are as follows:
The screenshot of the running effect is as follows:
The picture above has 3 states, the default state (gray and white), the mouse hover state (green), and the mouse click state (yellow). How is it achieved?
The Html code is as follows:
<table> <thead> <tr> <td>编号</td> <td>姓名</td> <td>年龄</td> <td>操作</td> </tr> </thead> <tbody> <tr> <td>1111</td> <td>1111</td> <td>1111</td> <td><input type="button" value="查看" /><input type="button" value="删除" /></td> </tr> <tr> <td>2222</td> <td>2222</td> <td>2222</td> <td><input type="button" value="查看" /><input type="button" value="删除" /></td> </tr> <tr> <td>3333</td> <td>3333</td> <td>3333</td> <td><input type="button" value="查看" /><input type="button" value="删除" /></td> </tr> <tr> <td>4444</td> <td>4444</td> <td>4444</td> <td><input type="button" value="查看" /><input type="button" value="删除" /></td> </tr> <tr> <td>5555</td> <td>5555</td> <td>5555</td> <td><input type="button" value="查看" /><input type="button" value="删除" /></td> </tr> </tbody> </table>
The plug-in implementation code is as follows:
(function () { $.fn.TabStyle = function (options) { //默认参数设置 var settings = { evenClass: "tab_even", //偶数行样式 oddClass: "tab_odd", //奇数行样式 hoverClass: "tab_hover", //鼠标悬浮样式 clickClass: "tab_click", //鼠标点击样式 isClick: true //是否开启鼠标点击样式 }; //合并参数 $.extend(settings, options); return this.each(function () { //为奇偶行分别添加样式 $(" > tbody > tr:even", this).addClass(settings.evenClass); $(" > tbody > tr:odd", this).addClass(settings.oddClass); $(" > tbody > tr", this).each(function (i) { //鼠标悬浮样式 $(this).hover(function () { $(this).addClass(settings.hoverClass); }, function () { $(this).removeClass(settings.hoverClass); }); //鼠标点击样式 if (settings.isClick) { $(this).bind("click", function () { $(this).addClass(settings.clickClass).siblings("tr").removeClass(settings.clickClass); }); } }); }); } })();
Sometimes we may not need the style after mouse click, so we set isClick as the control switch. If you don't want click styles, just set it to false.
DEMO is as follows:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>表格样式(银光棒)</title> <style type="text/css"> table{ width:700px; border:1px solid green;border-collapse:collapse;} table td{height:40px; text-align:center; width:25%;} .tab_even{ background-color: #DDD;} .tab_odd{ background-color: White;} .tab_hover{ background-color: Green;color:White;} .tab_click{ background-color: Orange;} </style> </head> <body> <table> <thead> <tr> <td>编号</td> <td>姓名</td> <td>年龄</td> <td>操作</td> </tr> </thead> <tbody> <tr> <td>1111</td> <td>1111</td> <td>1111</td> <td><input type="button" value="查看" /><input type="button" value="删除" /></td> </tr> <tr> <td>2222</td> <td>2222</td> <td>2222</td> <td><input type="button" value="查看" /><input type="button" value="删除" /></td> </tr> <tr> <td>3333</td> <td>3333</td> <td>3333</td> <td><input type="button" value="查看" /><input type="button" value="删除" /></td> </tr> <tr> <td>4444</td> <td>4444</td> <td>4444</td> <td><input type="button" value="查看" /><input type="button" value="删除" /></td> </tr> <tr> <td>5555</td> <td>5555</td> <td>5555</td> <td><input type="button" value="查看" /><input type="button" value="删除" /></td> </tr> </tbody> </table> <script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script src="../Scripts/jquery.similar.TabStyle.js" type="text/javascript"></script> <script type="text/javascript"> $("table").TabStyle(); </script> </body> </html>
Through the detailed code above, you should be able to use jquery to customize the table style. The editor's table style is not perfect yet and needs to be improved. I hope that everyone will continue to innovate and make a new one based on the completion of this style. Your own form.