枫云教育
您的当前位置:首页JQuery操作table中tr的位置

JQuery操作table中tr的位置

来源:枫云教育


这次给大家带来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中的树形关于取值传值详解

显示全文