jquery自定義表格樣式
本文實(shí)例講述了jquery自定義表格樣式實(shí)現(xiàn)代碼。分享給大家供大家參考。具體如下:
運(yùn)行效果截圖如下:

上面這張圖有3種狀態(tài),默認(rèn)狀態(tài)(灰白相間),鼠標(biāo)懸浮狀態(tài)(綠色),鼠標(biāo)點(diǎn)擊狀態(tài)(黃色),是如何實(shí)現(xiàn)的吶?
Html代碼如下:
<table>
<thead>
<tr>
<td>編號(hào)</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>
插件實(shí)現(xiàn)代碼如下:
(function () {
$.fn.TabStyle = function (options) {
//默認(rèn)參數(shù)設(shè)置
var settings = {
evenClass: "tab_even", //偶數(shù)行樣式
oddClass: "tab_odd", //奇數(shù)行樣式
hoverClass: "tab_hover", //鼠標(biāo)懸浮樣式
clickClass: "tab_click", //鼠標(biāo)點(diǎn)擊樣式
isClick: true //是否開啟鼠標(biāo)點(diǎn)擊樣式
};
//合并參數(shù)
$.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) {
//鼠標(biāo)懸浮樣式
$(this).hover(function () {
$(this).addClass(settings.hoverClass);
}, function () {
$(this).removeClass(settings.hoverClass);
});
//鼠標(biāo)點(diǎn)擊樣式
if (settings.isClick) {
$(this).bind("click", function () {
$(this).addClass(settings.clickClass).siblings("tr").removeClass(settings.clickClass);
});
}
});
});
}
})();
有些時(shí)候我們可能并不需要鼠標(biāo)點(diǎn)擊后的樣式,因此設(shè)置了isClick這個(gè)作為控制開關(guān)。如果不想要點(diǎn)擊樣式,將其設(shè)置為false即可。
DEMO如下:
<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>編號(hào)</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>
通過上文詳細(xì)的代碼,大家應(yīng)該會(huì)利用jquery自定義表格樣式了,小編的表格樣式還不夠完美,還需要進(jìn)行改進(jìn),希望大家在完成這個(gè)樣式的基礎(chǔ)上,繼續(xù)創(chuàng)新,做一個(gè)屬于自己的表格。
- jQuery實(shí)現(xiàn)獲取table表格第一列值的方法
- jQuery插件實(shí)現(xiàn)表格隔行變色及鼠標(biāo)滑過高亮顯示效果代碼
- 基于jQuery實(shí)現(xiàn)的無刷新表格分頁實(shí)例
- 基于jQuery實(shí)現(xiàn)點(diǎn)擊最后一行實(shí)現(xiàn)行自增效果的表格
- jQuery實(shí)現(xiàn)的調(diào)整表格行tr上下順序
- jquery調(diào)整表格行tr上下順序?qū)嵗v解
- 基于jquery實(shí)現(xiàn)表格無刷新分頁
- jquery實(shí)現(xiàn)表格隔行換色效果
- jQuery操作表格(table)的常用方法、技巧匯總
- 使用jQuery操作HTML的table表格的實(shí)例解析
相關(guān)文章
JQuery處理json與ajax返回JSON實(shí)例代碼
json數(shù)據(jù)是一種經(jīng)型的實(shí)時(shí)數(shù)據(jù)交互的數(shù)據(jù)存儲(chǔ)方法,使用到最多的應(yīng)該是ajax與json配合使用了,下面我來給大家介紹jquery處理json數(shù)據(jù)方法2014-01-01
jQuery實(shí)現(xiàn)Twitter的自動(dòng)文字補(bǔ)齊特效
本文介紹了一款jQuery實(shí)現(xiàn)的文字自動(dòng)補(bǔ)全特效的插件,該插件可以結(jié)合本地?cái)?shù)據(jù)進(jìn)行一些操作。推薦關(guān)注一下H5的幾種數(shù)據(jù)存儲(chǔ)的方式(localstorage與sessionstorage、IndexedDB、離線緩存manifest文件)2014-11-11
Jquery實(shí)現(xiàn)地鐵線路指示燈提示牌效果的方法
這篇文章主要介紹了Jquery實(shí)現(xiàn)地鐵線路指示燈提示牌效果的方法,實(shí)例分析了jQuery動(dòng)態(tài)顯示特效的使用技巧,需要的朋友可以參考下2015-03-03
jquery計(jì)算鼠標(biāo)和指定元素之間距離的方法
這篇文章主要介紹了jquery計(jì)算鼠標(biāo)和指定元素之間距離的方法,涉及jQuery針對頁面位置屬性與鼠標(biāo)事件的相關(guān)操作技巧,需要的朋友可以參考下2015-06-06
jQuery刪除節(jié)點(diǎn)的三個(gè)方法即remove()detach()和empty()
jQuery提供了三種刪除節(jié)點(diǎn)的方法,即remove(),detach()和empty(),下面為大家詳細(xì)介紹下jQuery刪除節(jié)點(diǎn)三個(gè)方法的具體使用2013-12-12
jQuery中的.bind()、.live()和.delegate()之間區(qū)別分析
jQuery中的.bind()、.live()和.delegate()之間區(qū)別分析,學(xué)習(xí)jquery的朋友可以參考下。2011-06-06

