首页 > Web开发 > 详细

JQuery实现表格行的上移、下移、删除、增加

时间:2015-05-14 18:08:25      阅读:275      评论:0      收藏:0      [点我收藏+]
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP ‘index.jsp‘ starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <script type="text/javascript" src="/Test/js/jquery.min.js"></script>
    <script type="text/javascript">
    //删除
    function del(t){
      $(t).parent().parent().remove();
    }
    
    //上移
    function up(t){
     var i=$(t).parent().parent().index();//当前行的id
     if(i>1){//不是表头,也不是第一行,则可以上移
       var tem0=$(t).parent().parent().html();
       var tem1=$(t).parent().parent().prev().html();
       $(t).parent().parent().prev().html(tem0);
       $(t).parent().parent().html(tem1);
     }
    }
    //下移
    function down(t){
     var l=$("#MyTB tr").length;//总行数
     var i=$(t).parent().parent().index();//当前行的id
     if(i<l-1){//不是最后一行,则可以下移
       var tem0=$(t).parent().parent().html();
       var tem1=$(t).parent().parent().next().html();
       $(t).parent().parent().next().html(tem0);
       $(t).parent().parent().html(tem1);
     }
    }   
    function add(t){
     var tem0=$(t).parent().parent().html();
     $(t).parent().parent().append("<tr>"+tem0+"</tr>");
    }
    </script>
  </head>
  <body>
    <table id="MyTB">
      <tr>
        <td>xxxxxxxx</td>
        <td>xxxxxxxx</td>
        <td>xxxxxxxx</td>
        <td>xxxxxxxx</td>
        <td>
        </td>
      </tr>
      <tr>
        <td>11111111</td>
        <td>11111111</td>
        <td>11111111</td>
        <td>11111111</td>
        <td>
        <input type="button" value="上" onclick="up(this)" />
        <input type="button" value="下" onclick="down(this)" />
        <input type="button" value="删" onclick="del(this)" />
         <input type="button" value="加" onclick="add(this)" />
        </td>
      </tr>
      <tr>
        <td>22222222</td>
        <td>22222222</td>
        <td>22222222</td>
        <td>22222222</td>
        <td>
        <input type="button" value="上" onclick="up(this)" />
        <input type="button" value="下" onclick="down(this)" />
        <input type="button" value="删" onclick="del(this)" />
         <input type="button" value="加" onclick="add(this)" />
        </td>
      </tr>
      <tr>
        <td>33333333</td>
        <td>33333333</td>
        <td>33333333</td>
        <td>33333333</td>
        <td>
        <input type="button" value="上" onclick="up(this)" />
        <input type="button" value="下" onclick="down(this)" />
        <input type="button" value="删" onclick="del(this)" />
         <input type="button" value="加" onclick="add(this)" />
        </td>
      </tr>
      <tr>
        <td>44444444</td>
        <td>44444444</td>
        <td>44444444</td>
        <td>44444444</td>
        <td>
        <input type="button" value="上" onclick="up(this)" />
        <input type="button" value="下" onclick="down(this)" />
        <input type="button" value="删" onclick="del(this)" />
         <input type="button" value="加" onclick="add(this)" />
        </td>
      </tr>
      <tr>
        <td>55555555</td>
        <td>55555555</td>
        <td>55555555</td>
        <td>55555555</td>
        <td>
        <input type="button" value="上" onclick="up(this)" />
        <input type="button" value="下" onclick="down(this)" />
        <input type="button" value="删" onclick="del(this)" />
         <input type="button" value="加" onclick="add(this)" />
        </td>
      </tr>
    </table>
  </body>
</html>

 

JQuery实现表格行的上移、下移、删除、增加

原文:http://www.cnblogs.com/XueRong-7/p/4503869.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!