@OverWrite BlogJava

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            128 隨筆 :: 0 文章 :: 29 評(píng)論 :: 0 Trackbacks
          應(yīng)用jquery,制作一個(gè)可以自動(dòng)添加刪除行的表格,演示地址 http://www.vesung.cn/demo/table.html
          下面是完整的代碼:
          <html>
           
          <head>
            
          <title>帶有添加刪除行功能的表格</title>
            
          <script type="text/javascript" src="../js/jquery-1.2.1.pack.js"></script>
          <script type="text/javascript">
              
          //為每個(gè)表格添加“添加”,“刪除”行的按鈕        
              function add_bt(){
                  
          //添加行按鈕定義
                  var bt_add = $("<input class='bt_add' type='button' value='add'/>");
                  
          //刪除行按鈕定義
                  var bt_del = $("<input class='bt_del' type='button' value='del'/>");
                  
          //插入按鈕
                  $('table').before(bt_del).before('&nbsp;').before(bt_add);
                  
          //為每個(gè)表格的每個(gè)添加行按鈕關(guān)聯(lián)click動(dòng)作
                  //以每個(gè)表格的最后一個(gè)tr元素為模板復(fù)制并append到表格內(nèi)
                  $('.bt_add').click(function(){
                      
          //this指按鈕元素
                      var last_tr = $(this).next('table').find('tr:last');
                      last_tr.after('
          <tr>'+last_tr.html()+'</tr>');
                      
          var table = $(this).next('table');
                      
          //最優(yōu)為表格排序
                      mark_index(table[0].id);
                  }
          );
                  
          //為每個(gè)表格的每個(gè)刪除行按鈕關(guān)聯(lián)click動(dòng)作
                  $('.bt_del').click(function(){
                      
          var last_tr = $(this).next().next('table').find('tr:last');
                      
          var index = last_tr[0].rowIndex;
                      
          if(last_tr[0].rowIndex>0){
                          last_tr.remove();
                          
          var table = $(this).next().next('table');
                          mark_index(table[
          0].id);
                      }

                  }
          );
              }

              
              
          //為每個(gè)表格添加序號(hào)
              function mark_index(table_id){
                  
          //定義序號(hào)的名稱
                  var indexName = '序號(hào)';
                  
          //獲得tables對(duì)象,如果table_id不為空,則返回指定的table元素,否則放回全部table元素集合
                  var tables = typeof table_id == "string"?$('#'+table_id):$('table');
                  
          //遍歷tables并執(zhí)行匿名函數(shù)
                  tables.each(function(){
                      
          var xuhao = $(this).find('tr:first').find('th:first').text();
                      
          //添加序號(hào)列
                      if(xuhao != indexName){
                          $(
          this).find('tr').each(function(){
                              
          if(this.rowIndex == 0)
                                  $(
          this.firstChild).before('<th align=center>'+indexName+'</th>');
                              
          else
                                  $(
          this.firstChild).before('<td align=center>'+this.rowIndex+'</td>');
                          }
          );                
                      }
          else{
                      
          //已經(jīng)有序號(hào)了,只需更新
                          $(this).find('tr').each(function(){
                              
          if(this.rowIndex != 0)
                                  $(
          this.firstChild).text(this.rowIndex);
                          }
          );    
                      }

                  }
          );        
              }

          </script>

          <script type="text/javascript">
              
          /**
               *dom文檔裝載完畢時(shí)執(zhí)行
               
          */

              $(document).ready(
          function(){
                  mark_index();
          //這里自動(dòng)執(zhí)行2個(gè)動(dòng)作:1.為每個(gè)表格添加序號(hào)
                  add_bt();        //2.在每個(gè)表格前添加 添加,刪除行的按鈕
              }
          );
          </script>
          </head>
           
          <body>

          <table id='t1' cellSpacing=0 cellPadding=0 border=1>
              
          <thead>
                  
          <tr><th>name</th><th>email</th><th>nishen</th></tr>
              
          </thead>
              
          <tbody>
                  
          <tr><td>&nbsp;1</td><td>&nbsp;56</td><td>&nbsp;</td></tr>
                  
          <tr><td>&nbsp;1</td><td>&nbsp;ds</td><td>&nbsp;</td></tr>
                  
          <tr><td>&nbsp;4</td><td>&nbsp;43</td><td>&nbsp;</td></tr>
                  
          <tr><td>&nbsp;5</td><td>&nbsp;2</td><td>&nbsp;</td></tr>
                  
          <tr><td>&nbsp;4</td><td>&nbsp;g</td><td>&nbsp;</td></tr>
              
          </toboby>
          </table>

          <br><br>    
          <table id='t2' cellSpacing=0 cellPadding=0 border=1>
              
          <thead>
                  
          <tr><th>name</th><th>email</th><th>nishen</th></tr>
              
          </thead>
              
          <tbody>
                  
          <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
                  
          <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
                  
          <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
                  
          <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
                  
          <tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
              
          </toboby>
          </table> 

          </body>
          </html>

          posted on 2008-02-22 14:45 vesung 閱讀(4012) 評(píng)論(0)  編輯  收藏 所屬分類: Ajax/html
          主站蜘蛛池模板: 丹东市| 大姚县| 西乌珠穆沁旗| 锦州市| 东乡族自治县| 玉林市| 深泽县| 江山市| 福贡县| 丰镇市| 繁昌县| 通山县| 福清市| 杭锦后旗| 龙海市| 宿松县| 邯郸县| 房山区| 吴忠市| 松溪县| 饶阳县| 府谷县| 德江县| 东山县| 吴忠市| 辽宁省| 日土县| 通榆县| 合江县| 南陵县| 桂东县| 如皋市| 惠来县| 洛阳市| 吐鲁番市| 宁国市| 芜湖县| 顺昌县| 泾阳县| 威远县| 武隆县|