JS实现的表格行上下移动操作示例
网络编程 2021-07-04 20:00www.168986.cn编程入门
这篇文章主要介绍了JS实现的表格行上下移动操作,涉及javascript针对页面元素节点与属性的相关操作技巧,需要的朋友可以参考下
本文实例讲述了JS实现表格行上下移动操作的方法。分享给大家供大家参考,具体如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://.w3./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)" onclick="moveUp(this)">上移</a></td> <td width="25%"><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td> </tr> <tr> <td>2</td> <td>22</td> <td><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td> <td><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td> </tr> <tr> <td>3</td> <td>33</td> <td><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td> <td><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td> </tr> <tr> <td>4</td> <td>44</td> <td><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td> <td><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td> </tr> <tr> <td>5</td> <td>55</td> <td><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td> <td><a href="javascript:void(0)" onclick="moveDown(this)">下移</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程序设计有所帮助。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程