jQuery 克隆倒数第二行并插入到表末尾
我正在编写一些代码,以便在单击时动态地将更多行添加到表中。我想克隆表的最后两行,然后将它们附加到末尾。当然,该表是动态的,因此没有固定的行数。我已经成功克隆了最后一行,但无法获得倒数第二行。我该如何选择它?
$('.additional_row').live('click', function() {
var $rows = $('#maintable tr');
var $secondLastRow = $rows[$rows.length - 2];
$('#maintable tbody>tr:nth-child(' + $secondLastRow + ')').clone(true).insertAfter('#maintable tbody>tr:last');
$('#maintable tbody>tr:last').clone(true).insertAfter('#maintable tbody>tr:last');
return false;
});
I'm making some code to dynamically add more rows to a table on click. I want to clone the last two rows of my table and then append them at the end. Of course, the table is dynamic so there is not a fixed number of rows. I have got the last row cloning okay but I can't get the second last row. How would I select it?
$('.additional_row').live('click', function() {
var $rows = $('#maintable tr');
var $secondLastRow = $rows[$rows.length - 2];
$('#maintable tbody>tr:nth-child(' + $secondLastRow + ')').clone(true).insertAfter('#maintable tbody>tr:last');
$('#maintable tbody>tr:last').clone(true).insertAfter('#maintable tbody>tr:last');
return false;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$('#maintable tbody>tr:last').prev('tr')
将为您提供倒数第二个$('#maintable tbody>tr:last').prev('tr')
will give you the second last one