JQuery实现表格数据行上移与下移
短信预约 -IT技能 免费直播动态提醒
本文实例为大家分享了JQuery实现表格数据行上移与下移的具体代码,供大家参考,具体内容如下
效果展示
代码实现
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bootstrap 实例 - 条纹表格</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script class="lazy" data-src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
<script class="lazy" data-src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style type="text/css">
.moveUpOrDown {
background-color: #5BC0DE;
border-radius: 3px;
cursor: pointer;
color: #FFFFFF;
padding: 2px;
font-size: 12px;
}
</style>
<script type="text/javascript">
$(function () {
$("body").on("click", ".moveUpOrDown", function () {
var text = $(this).text();
if (text == "上移") {
var prevTr = $(this).parent().parent().prevAll();
if (prevTr.length > 0) {
var preTemp = prevTr.first();
var thisHr = $(this).parent().parent();
thisHr.replaceWith("<tr>" + preTemp.html() + "</tr>");
preTemp.replaceWith("<tr>" + thisHr.html() + "</tr>");
}
} else if (text == "下移") {
var nextTr = $(this).parent().parent().next();
if (nextTr.length > 0) {
var thisHr = $(this).parent().parent();
thisHr.replaceWith("<tr>" + nextTr.html() + "</tr>");
nextTr.replaceWith("<tr>" + thisHr.html() + "</tr>");
}
}
});
});
</script>
</head>
<body>
<table class="table table-striped">
<caption>条纹表格布局</caption>
<thead>
<tr>
<th>名称</th>
<th>城市</th>
<th>邮编</th>
<th>排序</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tanmay1</td>
<td>Bangalore</td>
<td>560001</td>
<td><span class="moveUpOrDown">上移</span> | <span class="moveUpOrDown">下移</span></td>
</tr>
<tr>
<td>Sachin1</td>
<td>Mumbai</td>
<td>400003</td>
<td><span class="moveUpOrDown">上移</span> | <span class="moveUpOrDown">下移</span></td>
</tr>
<tr>
<td>Uma1</td>
<td>Pune</td>
<td>411027</td>
<td><span class="moveUpOrDown">上移</span> | <span class="moveUpOrDown">下移</span></td>
</tr>
<tr>
<td>Tanmay2</td>
<td>Bangalore</td>
<td>560001</td>
<td><span class="moveUpOrDown">上移</span> | <span class="moveUpOrDown">下移</span></td>
</tr>
<tr>
<td>Sachin2</td>
<td>Mumbai</td>
<td>400003</td>
<td><span class="moveUpOrDown">上移</span> | <span class="moveUpOrDown">下移</span></td>
</tr>
<tr>
<td>Uma2</td>
<td>Pune</td>
<td>411027</td>
<td><span class="moveUpOrDown">上移</span> | <span class="moveUpOrDown">下移</span></td>
</tr>
<tr>
<td>Tanmay3</td>
<td>Bangalore</td>
<td>560001</td>
<td><span class="moveUpOrDown">上移</span> | <span class="moveUpOrDown">下移</span></td>
</tr>
<tr>
<td>Sachin3</td>
<td>Mumbai</td>
<td>400003</td>
<td><span class="moveUpOrDown">上移</span> | <span class="moveUpOrDown">下移</span></td>
</tr>
<tr>
<td>Uma3</td>
<td>Pune</td>
<td>411027</td>
<td><span class="moveUpOrDown">上移</span> | <span class="moveUpOrDown">下移</span></td>
</tr>
</tbody>
</table>
</body>
</html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341