JS实现的表格行上下移动操作示例_Javascript教程-查字典教程网
JS实现的表格行上下移动操作示例
JS实现的表格行上下移动操作示例
发布时间:2016-12-30 来源:查字典编辑
摘要:本文实例讲述了JS实现表格行上下移动操作的方法。分享给大家供大家参考,具体如下:表格行移动111上移下移222上移下移333上移下移444上...

本文实例讲述了JS实现表格行上下移动操作的方法。分享给大家供大家参考,具体如下:

<!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> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>表格行移动</title> </head> <body> <table width="200" border="1"> <tbody> <tr> <td width="25%">1</td> <td width="25%">11</td> <td width="25%"><a href="javascript:void(0)">上移</a></td> <td width="25%"><a href="javascript:void(0)">下移</a></td> </tr> <tr> <td>2</td> <td>22</td> <td><a href="javascript:void(0)">上移</a></td> <td><a href="javascript:void(0)">下移</a></td> </tr> <tr> <td>3</td> <td>33</td> <td><a href="javascript:void(0)">上移</a></td> <td><a href="javascript:void(0)">下移</a></td> </tr> <tr> <td>4</td> <td>44</td> <td><a href="javascript:void(0)">上移</a></td> <td><a href="javascript:void(0)">下移</a></td> </tr> <tr> <td>5</td> <td>55</td> <td><a href="javascript:void(0)">上移</a></td> <td><a href="javascript:void(0)">下移</a></td> </tr> </tbody> </table> <script type="text/javascript"> <!-- function moveUp(_a){ var _row = _a.parentNode.parentNode; //如果不是第一行,则与上一行交换顺序 var _node = _row.previousSibling; while(_node && _node.nodeType != 1){ _node = _node.previousSibling; } if(_node){ swapNode(_row,_node); } } function moveDown(_a){ var _row = _a.parentNode.parentNode; //如果不是最后一行,则与下一行交换顺序 var _node = _row.nextSibling; while(_node && _node.nodeType != 1){ _node = _node.nextSibling; } if(_node){ swapNode(_row,_node); } } function swapNode(node1,node2){ //获取父结点 var _parent = node1.parentNode; //获取两个结点的相对位置 var _t1 = node1.nextSibling; var _t2 = node2.nextSibling; //将node2插入到原来node1的位置 if(_t1)_parent.insertBefore(node2,_t1); else _parent.appendChild(node2); //将node1插入到原来node2的位置 if(_t2)_parent.insertBefore(node1,_t2); else _parent.appendChild(node1); } //--> </script> </body> </html>

运行效果截图如下:

希望本文所述对大家JavaScript程序设计有所帮助。

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类