[英]table append without last element
<table id="table">
<tr><td>one</td><td>two</td></tr>
<tr id="sum"><td>sum</td><td>sum2</td></tr>
</table>
<span id="add">add new</span>
$('#add').click(function(){
$('#table').append('<tr><td>one</td><td>two</td></tr>');
})
How can i make that this add new element with append as penultimate element? 我怎样才能使这个添加新元素并附加为倒数第二个元素? I would like that #sum still are last element.
我希望#sum仍然是最后一个元素。
Use insertBefore()
: 使用
insertBefore()
:
$('#add').click(function(){
$('<tr><td>one</td><td>two</td></tr>').insertBefore('#sum');
})
You could also simply use multiple tbody
elements, one to contain the #sum
row, and the others for the, in this example, #content
: 您也可以简单地使用多个
tbody
元素,一个包含#sum
行,另一个用于#content
,在本例中为:
<table id="table">
<tbody id="content">
<tr><td>one</td><td>two</td></tr>
</tbody>
<tbody>
<tr id="sum"><td>sum</td><td>sum2</td></tr>
</tbody>
</table>
With the jQuery: 使用jQuery:
$('#add').click(function(){
$('#content').append('<tr><td>one</td><td>two</td></tr>');
})
Or, possibly, using a tfoot
(though I'm not sure that this a proper use-case for a tfoot
element: 或者,可能的话,使用
tfoot
(虽然我不知道这一个适当的用例的tfoot
元素:
<table id="table">
<tfoot>
<tr id="sum"><td>sum</td><td>sum2</td></tr>
</tfoot>
<tbody id="content">
<tr><td>one</td><td>two</td></tr>
</tbody>
</table>
And jQuery: 和jQuery:
$('#add').click(function(){
$('table tbody').append('<tr><td>one</td><td>two</td></tr>');
})
References: 参考文献:
You can use .before()
您可以使用
.before()
$('#add').click(function(){
$('#sum').before('<tr><td>one</td><td>two</td></tr>');
})
Really simple :) use before
真的很简单:)使用
before
$('#add').click(function(){
$('#sum').before('<tr><td>one</td><td>two</td></tr>');
});
做就是了:
$('#sum').before('<tr><td>one</td><td>two</td></tr>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.