欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解jQuery簡單的表格應(yīng)用

 更新時間:2016年12月16日 11:56:24   作者:老板丶魚丸粗面  
本文主要對表格的簡單應(yīng)用:表格變色;表格展開關(guān)閉;表格內(nèi)容篩選進(jìn)行實例分析介紹。具有很好的參考價值,需要的朋友一起來看下吧

大致介紹

在CSS技術(shù)之前,網(wǎng)頁的布局基本都是依靠表格制作,當(dāng)有了CSS之后,表格就被很多設(shè)計師所拋棄,但是表格也有他的用武之地,比如數(shù)據(jù)列表,下面以表格中常見的幾個應(yīng)用來加深對jQuery的認(rèn)識。

表格變色

基本的結(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、單選框控制表格行高亮

在每一行之前加一個單選按鈕,當(dāng)單擊某一行后,此行被選中高亮顯示并且單選框被選中

$('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>

添加事件,當(dāng)點擊一個分類的標(biāo)題時,這個分類關(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();
 });

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

最新評論