DIV CSS 佈局教程網

詳解 jQuery 在表格上的幾種實用應用
編輯:JQuery入門技巧     

大致介紹

在 CSS 技術普及之前,網頁布局幾乎都靠表格完成;有了 CSS 之後,表格雖然被許多設計師用於布局的場景減少,但在資料列表這類場景中仍佔有一席之地。以下就透過幾個表格中常見的應用,來加深對 jQuery 的認識。

表格變色

基本的結構:

 <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、複選框控制表格行高亮

 $('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); } });

表格展開關閉

基本結構:

 <table> <thead> <tr><th></th><th>姓名</th><th>性別</th><th>暫住地</th></tr> </thead> <tbody> <tr class="parent" id="row_01"><td colspan="3">前台設計組</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">前台開發組</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">後台開發組</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> 

添加事件,當點擊某個分類的標題時,該分類便展開或關閉:

 $('tr.parent').click(function(){ $(this).toggleClass('selected') .siblings('.child_' + this.id).toggle(); });

表格內容篩選

基本結構:

 <table> <thead> <tr><th></th><th>姓名</th><th>性別</th><th>暫住地</th></tr> </thead> <tbody> <tr class="parent" id="row_01"><td colspan="3">前台設計組</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">前台開發組</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">後台開發組</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(); }); 

以上就是本次分享的全部內容,希望對你的學習或工作有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved