删除表格行并将相同的表格行添加到表格末尾

3
我希望实现以下功能:需要从id为#emailTable的表格中删除一个表格行,然后将完全相同的行追加到表格末尾,并使用JQuery隐藏追加的表格行。
要删除表格行,可以按照以下步骤进行:
$("#rowid_"+id).remove();
3个回答

9
假设 #rowid-id 是指 <tr> 元素,那么只需执行以下操作:
$("#rowid_"+id).appendTo('table');

这将把你的行元素移动到table的最后一个元素之后。
看一下这个示例

我喜欢@Boris Guéry的解决方案...快速简单而且直接。这种方法有什么缺点吗? - whispers

3
var a;
a = $("#rowid_"+id).html(); // store the content on a
$("#rowid_"+id).remove(); // remove the node
$('table').append(a);// append the content on table
$("#rowid_"+id).hide();//hide the appended node

我希望你能够正常工作 :)

3
它有效了,我采用了你的答案并稍作修改:var row = $("#rowid_"+id).clone().appendTo("#emailTable").hide(); $("#rowid_"+id).remove(); 然后它仍然有效。谢谢,我会接受你的答案。 - Elitmiar
Roland,克隆节点是非常聪明的解决方案,今天学到了新东西 :) - Prakash

1
如果页面中只有一个表格,Boris Guéry的回答是正确的。
以下是适用于页面中有多个表格的版本。
$("#tr_Row_" + id).appendTo($("#tr_Row_" + id).closest("table"));

解释:第二部分
$("#tr_Row_" + id).closest("table") // get the parent-table of the row.

解释:第一部分
$("#tr_Row_" + id).appendTo(...parentTable...) // remove row from the current table
                                               // and append it as last child to the 
                                               // parentTable

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接