abstract:本文主要對表格的簡單應(yīng)用:表格變色;表格展開關(guān)閉;表格內(nèi)容篩選進行實例分析介紹。大致介紹在CSS技術(shù)之前,網(wǎng)頁的布局基本都是依靠表格制作,當有了CSS之后,表格就被很多設(shè)計師所拋棄,但是表格也有他的用武之地,比如數(shù)據(jù)列表,下面以表格中常見的幾個應(yīng)用來加深對jQuery的認識。表格變色基本的結(jié)構(gòu):<table> <thead>
本文主要對表格的簡單應(yīng)用:表格變色;表格展開關(guān)閉;表格內(nèi)容篩選進行實例分析介紹。
大致介紹
在CSS技術(shù)之前,網(wǎng)頁的布局基本都是依靠表格制作,當有了CSS之后,表格就被很多設(shè)計師所拋棄,但是表格也有他的用武之地,比如數(shù)據(jù)列表,下面以表格中常見的幾個應(yīng)用來加深對jQuery的認識。
表格變色
基本的結(jié)構(gòu):
<table> <thead> <tr><th>姓名</th><th>性別</th><th>暫住地</th></tr> </thead> <tbody> <tr><td>張三</td><td>男</td><td>杭州</td></tr> <tr><td>王五</td><td>女</td><td>江蘇</td></tr> <tr><td>李斯</td><td>男</td><td>北京</td></tr> <tr><td>趙六</td><td>女</td><td>蘭州</td></tr> <tr><td>往往</td><td>男</td><td>酒泉</td></tr> <tr><td>李師傅</td><td>男</td><td>東京</td></tr> </tbody> </table>
1、普通的隔行變色
首先定義兩個樣式
.even{ background: #FFF38F; } .odd{ background: #FFFFEE; }
添加變色
$('tr:odd').addClass('odd'); $('tr:even').addClass('even');
2、單選框控制表格行高亮
在每一行之前加一個單選按鈕,當單擊某一行后,此行被選中高亮顯示并且單選框被選中
$('tbody>tr').click(function(){ $(this) .addClass('SELECTed') .siblings().removeClass('SELECTed') .end() .find(':radio').attr('checked',true); });
3、復(fù)選框控制表格行高亮
$('tbody>tr').click(function(){ if($(this).hasClass('SELECTed')){ $(this).removeClass('SELECTed') .find(':checkbox').attr('checked',false); }else{ $(this).addClass('SELECTed') .find(':checkbox').attr('checked',true); } });
表格展開關(guān)閉
基本結(jié)構(gòu):
<table> <thead> <tr><th></th><th>姓名</th><th>性別</th><th>暫住地</th></tr> </thead> <tbody> <tr class="parent" id="row_01"><td colspan="3">前臺設(shè)計組</td></tr> <tr class="child_row_01"><td></td><td>張三</td><td>男</td><td>杭州</td></tr> <tr class="child_row_01"><td></td><td>王五</td><td>女</td><td>江蘇</td></tr> <tr class="parent" id="row_02"><td colspan="3">前臺開發(fā)組</td></tr> <tr class="child_row_02"><td></td><td>李斯</td><td>男</td><td>北京</td></tr> <tr class="child_row_02"><td></td><td>趙六</td><td>女</td><td>蘭州</td></tr> <tr class="parent" id="row_03"><td colspan="3">后臺開發(fā)組</td></tr> <tr class="child_row_03"><td></td><td>往往</td><td>男</td><td>酒泉</td></tr> <tr class="child_row_03"><td></td><td>李師傅</td><td>男</td><td>東京</td></tr> </tbody> </table>
添加事件,當點擊一個分類的標題時,這個分類關(guān)閉或者打開
$('tr.parent').click(function(){ $(this).toggleClass('SELECTed') .siblings('.child_' + this.id).toggle(); });
表格內(nèi)容篩選
基本結(jié)構(gòu):
<table> <thead> <tr><th></th><th>姓名</th><th>性別</th><th>暫住地</th></tr> </thead> <tbody> <tr class="parent" id="row_01"><td colspan="3">前臺設(shè)計組</td></tr> <tr class="child_row_01"><td></td><td>張三</td><td>男</td><td>杭州</td></tr> <tr class="child_row_01"><td></td><td>王五</td><td>女</td><td>江蘇</td></tr> <tr class="parent" id="row_02"><td colspan="3">前臺開發(fā)組</td></tr> <tr class="child_row_02"><td></td><td>李斯</td><td>男</td><td>北京</td></tr> <tr class="child_row_02"><td></td><td>趙六</td><td>女</td><td>蘭州</td></tr> <tr class="parent" id="row_03"><td colspan="3">后臺開發(fā)組</td></tr> <tr class="child_row_03"><td></td><td>往往</td><td>男</td><td>酒泉</td></tr> <tr class="child_row_03"><td></td><td>李師傅</td><td>男</td><td>東京</td></tr> </tbody> </table> <input type="text" id="filterName" />
添加事件
$('#filterName').keyup(function(){ $('table tbody tr').hide().filter(":contains(' "+($(this).val())+" ' )").show(); });
更多關(guān)于jQuery簡單的表格應(yīng)用請關(guān)注PHP中文網(wǎng)(www.miracleart.cn)其它文章!