JQuery只克隆了一个thead表的每个tr

时间:2012-01-31 20:38:52

标签: jquery clone

我只想克隆html tr和th,但不能捕获外部的thead / thead元素。

<thead id="justCloneTR">   // don't clone
 <tr id="Vehicle_1">       // clone
  <th>1</th>              // clone
  <th>2</th>              // clone
 </tr>                    // clone
</thead>                  // don't clone

<div id="putCloneHere"></div>

JS

$('#justCloneTR').clone('tr').appendTo('#putCloneHere');

1 个答案:

答案 0 :(得分:4)

如果您id tr,那么您可以使用此功能。

$('#Vehicle_1').clone();

.clone()参考:http://api.jquery.com/clone/

请记住,在克隆之后,您应该在将其添加到DOM之前更改克隆元素的ID,因为您不应该有2个具有相同ID的元素。

试试这个。

var id = 'Vehicle_' + parseInt($('#Vehicle_1').attr('id').match(/\d+/g), 10) + 1;
$('#Vehicle_1').clone().attr('id', id).appendTo('#putCloneHere');