JQuery实现置顶、置底、向上、向下、取消排序功能

<!DOCTYPE html>
<html>
<head>
<title>sort.html</title>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
</head>
<body>
	<table border="1" cellpadding="10" cellspacing="0">
		<thead>
			<tr>
				<th>序号</th>
				<th>内容</th>
				<th>排序</th>
			</tr>
		</thead>
		<tbody class="table">
			<tr>
				<td>1</td>
				<td>内容一</td>
				<td><a href="javascript:void(0)" class="top">置顶</a> <a
					href="javascript:void(0)" class="upbtn">上</a> <a
					href="javascript:void(0)" class="downbtn">下</a> <a
					href="javascript:void(0)" class="bottom">置底</a></td>
			</tr>
			<tr>
				<td>2</td>
				<td>内容二</td>
				<td><a href="javascript:void(0)" class="top">置顶</a> <a
					href="javascript:void(0)" class="upbtn">上</a> <a
					href="javascript:void(0)" class="downbtn">下</a> <a
					href="javascript:void(0)" class="bottom">置底</a></td>
			</tr>
			<tr>
				<td>3</td>
				<td>内容三</td>
				<td><a href="javascript:void(0)" class="top">置顶</a> <a
					href="javascript:void(0)" class="upbtn">上</a> <a
					href="javascript:void(0)" class="downbtn">下</a> <a
					href="javascript:void(0)" class="bottom">置底</a></td>
			</tr>
			<tr>
				<td>4</td>
				<td>内容四</td>
				<td><a href="javascript:void(0)" class="top">置顶</a> <a
					href="javascript:void(0)" class="upbtn">上</a> <a
					href="javascript:void(0)" class="downbtn">下</a> <a
					href="javascript:void(0)" class="bottom">置底</a></td>
			</tr>
			<tr>
				<td>5</td>
				<td>内容五</td>
				<td><a href="javascript:void(0)" class="top">置顶</a> <a
					href="javascript:void(0)" class="upbtn">上</a> <a
					href="javascript:void(0)" class="downbtn">下</a> <a
					href="javascript:void(0)" class="bottom">置底</a></td>
			</tr>
		</tbody>
	</table>
	<a href="javascript:void(0)" οnclick="javascript:cancleRank();">取消排序</a>
</body>
<script type="text/javascript">
$(document).ready(function () {
	//上移
	var $upbtn = $(".upbtn")
	$upbtn.click(function() {
		var $tr = $(this).parents("tr");
		if ($tr.index() != 0) {
			$tr.fadeOut().fadeIn();
			$tr.prev().before($tr);
			$tr.css("color","#f60");
			colorcancel();
		}
	});
	//下移
	var $downbtn = $(".downbtn");
	var len = $downbtn.length;
	$downbtn.click(function() {
		var $tr = $(this).parents("tr");
		if ($tr.index() != len - 1) {
			$tr.fadeOut().fadeIn();
			$tr.next().after($tr);
			$tr.css("color","#f60");
			colorcancel();
		}
	});
	//置顶
	var $top = $(".top");
	$top.click(function(){
		var $tr = $(this).parents("tr");
		if ($tr.index() != 0) {
			$tr.fadeOut().fadeIn();
			$("tbody").prepend($tr);
			$tr.css("color","#f60");
			colorcancel();
		}
	});
	//置底
	var $bottom = $(".bottom");
	var length = $bottom.length;
	$bottom.click(function(){
		var $tr = $(this).parents("tr");
		if ($tr.index() != length-1) {
			$tr.fadeOut().fadeIn();
			$("tbody").append($tr);
			$tr.css("color","#f60");
			colorcancel();
		}
	});
});
//初始化列表
var initList = [], domArr = [];
$('tbody tr').each(function(a){
	initList[a] = $(this).html();
})
      //取出所有tr放进数组
      function getDom(){
          $('tbody tr').each(function( m ){
              domArr[m] = $(this);
          });
      }
//取消排序
function cancleRank(){
	getDom();
	$.each(initList,function(b){
		$.each(domArr,function(c){
			if(domArr[c].html() == initList[b]){
				$('table tbody').append(domArr[c]);
			}
		});
	});
	$('tbody tr').each(function(){
		$(this).css("color","");
	})
}
//取消颜色高亮
function colorcancel(){
	$('tbody tr').each(function(){
		var index = $(this).index()+1;
		var serialnumber = $(this).find("td:first").text();
		if(index == serialnumber){
			$(this).css("color","");
		}
	});
}
</script>
</html>



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值