jquery 对table中tr上下移动的控制

<html>
<head>
<title>jquery表格操作</title>
<script language="javascript" src="jquery.table.tr.js"></script>
</head>
<body>
            <a href="#" οnclick="add_line();">添加一行</a>
&nbsp;&nbsp;<a href="#" οnclick="remove_line();">删除一行</a>
&nbsp;&nbsp;<a href="#" οnclick="up_exchange_line();">上移</a>
&nbsp;&nbsp;<a href="#" οnclick="down_exchange_line();">下移</a>
&nbsp;&nbsp;<a href="#" οnclick="top_exchange_line();">置顶</a>
&nbsp;&nbsp;<a href="#" οnclick="hou_exchange_line();">置底</a>
<table>
<tr><td>序号</td><td>步骤名称</td><td>步骤描述</td><td>相关操作</td></tr>
</table>
<table id="content">
</table>
</body>
</html>
<script language="javascript">
var currentStep=0;
var max_line_num=0;
function add_line(){
  max_line_num=$("#content tr:last-child").children("td").html();
  if(max_line_num==null) {
    max_line_num=1;
  }
  else{
    max_line_num=parseInt(max_line_num);
	max_line_num+=1;
  }
  $('#content').append("<tr id='line"+max_line_num+"' οnclick='lineclick(this);'><td>"+max_line_num+"</td><td>打开网页"+max_line_num+"</td><td>打开登录网页"+max_line_num+"</td><td>删除&nbsp;&nbsp;编辑</td></tr>");
}
function remove_line(){ 
  if(currentStep==0){
    alert('请选择一项!');
	return false;
  }
  $("#content tr").each(
    function(){
	  var seq=parseInt($(this).children("td").html());
	  if(seq==currentStep) $(this).remove();
	  if(seq>currentStep) $(this).children("td").each(function(i){if(i==0)$(this).html(seq-1);});
	}
  );
  currentStep=0;
}

function top_exchange_line(){
  if(currentStep==0){
    alert('请选择一项!');
	return false;
  }
  if(currentStep<=1){
     alert('非法操作!');
	 return false;
  }
  var topStep = 1;
   //修改序号
  $('#line'+topStep+" td:first-child").html(currentStep);
  $('#line'+currentStep+" td:first-child").html(topStep);
  //取得两行的内容
  var topContent=$('#line'+topStep).html();
  var currentContent=$('#line'+currentStep).html();
  $('#line'+topStep).html(currentContent);
  //交换当前行与首行内容
  $('#line'+currentStep).html(topContent);  
  $('#content tr').each(function(){$(this).css("background-color","#ffffff");});
  $('#line'+topStep).css("background-color","yellow"); 
  currentStep=topStep;
}

function hou_exchange_line(){
 if(currentStep==0){
    alert('请选择一项!');
	return false;
 }
 if(currentStep>=max_line_num){
     alert('非法操作!');
	 return false;
  }
  var nextStep=max_line_num;
  //修改序号
  $('#line'+nextStep+" td:first-child").html(currentStep);
  $('#line'+currentStep+" td:first-child").html(nextStep);
  //取得两行的内容
  var nextContent=$('#line'+nextStep).html();
  var currentContent=$('#line'+currentStep).html();
  $('#line'+nextStep).html(currentContent);
  //交换当前行与尾行内容
  $('#line'+currentStep).html(nextContent);  
  $('#content tr').each(function(){$(this).css("background-color","#ffffff");});
  $('#line'+nextStep).css("background-color","yellow"); 
  currentStep=nextStep;
}

function up_exchange_line(){ 
  if(currentStep==0){
    alert('请选择一项!');
	return false;
  }
  if(currentStep<=1){
     alert('非法操作!');
	 return false;
  }
  var upStep=currentStep-1;
  //修改序号
  $('#line'+upStep+" td:first-child").html(currentStep);
  $('#line'+currentStep+" td:first-child").html(upStep);
  //取得两行的内容
  var upContent=$('#line'+upStep).html();
  var currentContent=$('#line'+currentStep).html();
  $('#line'+upStep).html(currentContent);
  //交换当前行与上一行内容
  $('#line'+currentStep).html(upContent);  
  $('#content tr').each(function(){$(this).css("background-color","#ffffff");});
  $('#line'+upStep).css("background-color","yellow"); 
  currentStep=upStep;
}
function down_exchange_line(){
 if(currentStep==0){
    alert('请选择一项!');
	return false;
 }
 if(currentStep>=max_line_num){
     alert('非法操作!');
	 return false;
  }
  var nextStep=parseInt(currentStep)+1;
  //修改序号
  $('#line'+nextStep+" td:first-child").html(currentStep);
  $('#line'+currentStep+" td:first-child").html(nextStep);
  //取得两行的内容
  var nextContent=$('#line'+nextStep).html();
  var currentContent=$('#line'+currentStep).html();
  $('#line'+nextStep).html(currentContent);
  //交换当前行与上一行内容
  $('#line'+currentStep).html(nextContent);  
  $('#content tr').each(function(){$(this).css("background-color","#ffffff");});
  $('#line'+nextStep).css("background-color","yellow"); 
  currentStep=nextStep;
}
function lineclick(line){
   $('#content tr').each(function(){$(this).css("background-color","#ffffff");});
   var seq=$(line).children("td").html();
   $(line).css("background-color","yellow");
   currentStep=seq;
}
 
</script>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现点击按钮来实现上下tr排序,可以按照以下步骤进行: 1. 给上下排序按钮添加点击事件,获取当前点击的按钮类型(上/下)。 2. 获取当前选的行,并根据按钮类型进行排序。 3. 根据排序结果,重新渲染表格。 下面是一个示例代码: HTML: ``` <table id="myTable"> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <tr> <td>张三</td> <td>25</td> <td>男</td> </tr> <tr> <td>李四</td> <td>30</td> <td>男</td> </tr> <tr> <td>王五</td> <td>20</td> <td>女</td> </tr> </tbody> </table> <button id="upBtn">上移</button> <button id="downBtn">下移</button> ``` jQuery: ``` $(function() { // 给上下排序按钮添加点击事件 $('#upBtn, #downBtn').click(function() { var type = $(this).attr('id') == 'upBtn' ? 'up' : 'down'; // 获取当前点击的按钮类型 var selected = $('#myTable tbody tr.selected'); // 获取当前选的行 if (selected.length) { if (type == 'up') { selected.prev().before(selected); // 上移 } else { selected.next().after(selected); // 下移 } } // 重新渲染表格 $('#myTable tbody tr').removeClass('selected'); $('#myTable tbody tr:odd').addClass('odd'); $('#myTable tbody tr:even').addClass('even'); }); // 给表格内容添加点击事件 $('#myTable tbody tr').click(function() { $(this).addClass('selected').siblings().removeClass('selected'); }); }); ``` 该代码实现了点击按钮来实现上下tr排序的功能,并且支持对选的行进行上下移动。需要注意的是,该代码只支持对相邻的行进行移动,对于非相邻的行需要进行相应的处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值