js~一个列表中包含上移下移删除等功能

时间:2021-09-10 16:00:38

最近做了一个项目,包括了一个列表页,为了用户体验,操作均使用JS实现,其中包括在列表中实现上移,下移,删除等功能,前台JS,后端数据修改使用AJAX,本文主要说一下前台JS这块

先看一下页面的截图

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYEAAAC/CAIAAACXPPcaAAASoUlEQVR4nO2cS2obSxtAaxLCHQfCP80ieg1ZQBZQiwn0IIOsIzQxuYiQiU0gC+hLyJ0ooxBPYmMo0EUgBAL9g37V46tSq91yIfschJHr8XWVoY6/qm5J7QEA8qFyDwAAnjQ4CABygoMAICc4CAByMt1Bv379ury8/PTpUwUAMJWJDqrrerFYLJfLm5sbAwAwlSkOur6+XiwWt7e3uQcPAGfPFAddXV0tl8vcIweAx8AUB11cXNzd3eUeOQA8BqY4qKqq3MMGgEcCDgKAnOAgAMjJSRyklFJKHb72uGbnzlFzfAp/EACbB3WQGoHfpy4LpfSxiVelu3h217osmsKirIPLaKusbyi0bCcyciAHW3pzt9tb7+uyiP0NElUAZ8BsDhpjFnulpQuNMZZIZBFEqcui6xJ964T0PFfp7jerjzNTcb7iWMY4KBZ5aFTp6N8gUQVwDsgOWq1W3759+/Pnz7EOSr/3SuL2sTORSQpyhKIr461V+5fWdENlXWlttxxieeM8KseJCUt0UDAfpZRSuhzcWWmlirLqq8iE4FwRHPTPP/+8ePFCKfX8+fMPHz7M7iC7PJpBtJaotLvA0klHSJ/IuOlC/1tb75jG695VhIKw5yhe3Wsgtvf+VmK0YXjtuyFTi40c4FwQHPTmzZt+qb969epYB3lyCZXheSSllbos3DToOAc5S9UK42ZK8pFKkx4FxQdn5zU2hwRkhw3jd8PrB1+XRVEMG0R/VwlwdggOev36db8MXr58Oc1BzZu3b9+O1I1Y1exCpv2fbzYrw8q1wggKkhey39CMyH1iLdMOCpu5U3EHX8hbRYBzRHDQly9fnj171hjh3bt3RznIM47noLCl9G9/wN+JjSW4l+bIJDBLfCH3doqNOW1VI5lF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ODACAnOAgAcoKDACAnOAgAcvJ/ClsBm+iEYtIAAAAASUVORK5CYII=" alt="" />

看一下它的HTML结构,当然,这与前台切图有关,后端程序人员只负责写自己的JS这块,我以我们项目为例,看一下它们切的HTML

<ul class="clearfix">
<li class="courseList">
<div class="titDefault clearfix">
<div class="left clearfix">
<span class="dragBtn"></span><span class="tit">内容<em class="contIndex">1</em>:</span><em title="2013年 加班.txt" class="titDefaultName">2013年 加班.txt</em>
</div>
<div class="mid">2014/9/24 9:54:00</div>
<div class="right clearfix">
<a value="253040" class="moveUpBtn" href="javascript:;"><span class="delTit">上移</span></a>
<a value="253040" class="moveDownBtn" href="javascript:;"><span class="delTit">下移</span></a>
<a value="253040" class="deleteBtn" href="javascript:;"><span class="delTit">删除</span></a>
</div>
</div>
</li>
<li class="courseList">
<div class="titDefault clearfix">
<div class="left clearfix">
<span class="dragBtn"></span><span class="tit">内容<em class="contIndex">2</em>:</span><em title="使用说明.txt" class="titDefaultName">使用说明.txt</em>
</div>
<div class="mid">2014/9/24 9:54:00</div>
<div class="right clearfix"> <a value="253041" class="moveUpBtn" href="javascript:;"><span class="delTit">上移</span></a>
<a value="253041" class="moveDownBtn" href="javascript:;"><span class="delTit">下移</span></a>
<a value="253041" class="deleteBtn" href="javascript:;"><span class="delTit">删除</span></a>
</div>
</div>
</li>
<li class="courseList">
<div class="titDefault clearfix">
<div class="left clearfix">
<span class="dragBtn"></span><span class="tit">内容<em class="contIndex">3</em>:</span><em title="占占大师.txt" class="titDefaultName">占占大师.txt</em>
</div>
<div class="mid">2014/9/24 9:54:00</div>
<div class="right clearfix">
<a value="253040" class="moveUpBtn" href="javascript:;"><span class="delTit">上移</span></a>
<a value="253040" class="moveDownBtn" href="javascript:;"><span class="delTit">下移</span></a>
<a value="253040" class="deleteBtn" href="javascript:;"><span class="delTit">删除</span></a>
</div>
</div>
</li>
<li class="courseList">
<div class="titDefault clearfix">
<div class="left clearfix">
<span class="dragBtn"></span><span class="tit">内容<em class="contIndex">4</em>:</span><em title="排序问题.txt" class="titDefaultName">排序问题.txt</em>
</div>
<div class="mid">2014/9/24 9:54:00</div>
<div class="right clearfix"> <a value="253041" class="moveUpBtn" href="javascript:;"><span class="delTit">上移</span></a>
<a value="253041" class="moveDownBtn" href="javascript:;"><span class="delTit">下移</span></a>
<a value="253041" class="deleteBtn" href="javascript:;"><span class="delTit">删除</span></a>
</div>
</div>
</li>
</ul>

下面我们主要看一下JS代码,主要使用JQ的on方法实现的,原因是因为列表的数据有第一次为静态的(bind),当排序后,数据变为动态的(live),所以,这种结构只能使用on才最合理,看一下代码

<script type="text/ecmascript">
$(function () {
//上移
$(".clearfix").on("click", ".moveUpBtn", function () {
var self = $(this);
var _old = self.closest("li.courseList");
var _new = self.closest("li.courseList").prev("li");
if (_new.length > 0) {
var _temp = _old.html();
_old.empty().append(_new.html());
_new.empty().append(_temp);
} }); //下移
$(".clearfix").on("click", ".moveDownBtn", function () {
var self = $(this);
var _old = self.closest("li.courseList");
var _new = self.closest("li.courseList").next("li");
if (_new.length > 0) {
var _temp = _old.html();
_old.empty().append(_new.html());
_new.empty().append(_temp);
}
}); //删除
$(".clearfix").on("click", ".deleteBtn", function () {
var self = $(this);//当前click事件源对象
self.closest("li.courseList").remove();
}); });
</script>

运行之后,效果就出来了,本JS中没有把与后台交互的AJAX方法写出来,大家可以根据具体情况而定。