jquery 行交換 上移 下移

來源:互聯網
上載者:User

標籤:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>表格式資料上下行互換位置</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
//上移
$("input.upbtn").each(function () {
$(this).click(function () {
var $tr = $(this).parents("tr");
if ($tr.index() != 0) {
$tr.prev().before($tr);
}
});
});
//下移
var trLength = $("input.downbtn").length; 
$("input.downbtn").each(function () {
$(this).click(function () {
var $tr = $(this).parents("tr"); 
if ($tr.index() != trLength - 1) { 
$tr.next().after($tr);
}
});
});
});

</script>
</head>
<body>
<table border="1" cellpadding=0 cellspacing=0>
<tr>
<td>4</td>
<td>xxx44xxxx</td>
<td>2013-5-24</td>
<td><input type="button" value="上移" class="upbtn" /><input type="button" value="下移" class="downbtn" /></td>
</tr>
<tr>
<td>5</td>
<td>xxxx55xxx</td>
<td>2013-5-25</td>
<td><input type="button" value="上移" class="upbtn" /><input type="button" value="下移" class="downbtn" /></td>
</tr>
<tr>
<td>6</td>
<td>xxxx66xxx</td>
<td>2013-5-26</td>
<td><input type="button" value="上移" class="upbtn" /><input type="button" value="下移" class="downbtn" /></td>
</tr>
<tr>
<td>7</td>
<td>xxxx77xxx</td>
<td>2013-5-27</td>
<td><input type="button" value="上移" class="upbtn" /><input type="button" value="下移" class="downbtn" /></td>
</tr>
<tr>
<td>8</td>
<td>xxx88xxxx</td>
<td>2013-5-28</td>
<td><input type="button" value="上移" class="upbtn" /><input type="button" value="下移" class="downbtn" /></td>
</tr>
</table>
</body>
</html>

 

 

<!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 行交換 上移 下移

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.