当前位置:Gxlcms > JavaScript > JQuery操作table中tr的位置

JQuery操作table中tr的位置

时间:2021-07-01 10:21:17 帮助过:3人阅读

这次给大家带来JQuery操作table中tr的位置,JQuery操作table中tr的位置的注意事项有哪些,下面就是实战案例,一起来看一下。

表格样式

<table>
  <tr>
    <td><input  type="button" value="上移" onclick="moveUp(this)"/></td>
    <td><input  type="button" value="下移" onclick="moveDown(this)"/></td>
  </tr>
  <tr>
    <td><input  type="button" value="上移" onclick="moveUp(this)"/></td>
    <td><input  type="button" value="下移" onclick="moveDown(this)"/></td>
  </tr>
    <tr>
    <td><input  type="button" value="上移" onclick="moveUp(this)"/></td>
    <td><input  type="button" value="下移" onclick="moveDown(this)"/></td>
  </tr>
</table>

js代码

// 上移 
function moveUp(obj) { 
  var current = $(obj).parent().parent(); //获取当前<tr>
  var prev = current.prev();  //获取当前<tr>前一个元素
  if (current.index() > 0) { 
    current.insertBefore(prev); //插入到当前<tr>前一个元素前
  } 
} 
// 下移 
function moveDown(obj) { 
  var current = $(obj).parent().parent(); //获取当前<tr>
  var next = current.next(); //获取当前<tr>后面一个元素
  if (next) { 
    current.insertAfter(next);  //插入到当前<tr>后面一个元素后面
  } 
}

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

jquery的插件怎么打印页面内容

jQuery运行页面怎样默认触发点击事件

layui中的树形关于取值传值详解

以上就是JQuery操作table中tr的位置的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行