如何移动< tr>在< table>中向上或向下通过jQuery吗?
问题描述:
执行此操作的优雅方法是什么?
What's the elegant way to do this?
答
这是我为您编写的一个快速插件. 在表上调用它,并为其指定旧行和新行的位置.
Here's a quick plugin I wrote for you. Call it on a table, and give it the the old row and the new row position.
$.fn.extend({
moveRow: function(oldPosition, newPosition) {
return this.each(function(){
var row = $(this).find('tr').eq(oldPosition).remove();
$(this).find('tr').eq(newPosition).before(row);
});
}
});
$('#myTable').moveRow(4, 3);
以下是jsbin的示例: http://jsbin.com/uroyi
Here's an example on jsbin: http://jsbin.com/uroyi