复制div并使用jQuery追加到一组元素中的另一个元素

时间:2017-05-02 15:20:03

标签: jquery html-table jquery-clone

我想克隆一个div并使用jQuery将其附加到HTML表中的另一个元素。例如

我想复制div section_title并立即将其附加到append_class范围内。换句话说,div section_title FIRST TITLE 将附加到范围append_class,同样。

这是HTML

<tr class="tr_class">
<th scope="row">
<div class="section_title">FIRST TITLE</div>
</th>
<td>
RANDOM CONTENT...
</td>
</tr>
<span class="append_class"></span>

<tr class="tr_class">
<th scope="row">
<div class="section_title">SECOND TITLE</div>
</th>
<td>
RANDOM CONTENT...
</td>
</tr>
<span class="append_class"></span>


<tr class="tr_class">
<th scope="row">
<div class="section_title">THIRD TITLE</div>
</th>
<td>
RANDOM CONTENT...
</td>
</tr>
<span class="append_class"></span>

我的尝试失败了。

var cloneTitle= $('.tr_class').find('.section_title').clone();
$('.append_class').html(cloneTitle);

它确实克隆但它克隆了所有section_title并相应地附加它们。

对此的任何指导将不胜感激。

提前致谢

1 个答案:

答案 0 :(得分:1)

如果我明白你的观点你需要使用.each()$(this)

$('.tr_class').each(function(){
   var cloneTitle = $(this).find('.section_title').clone();
    $(this).next('.append_class').html(cloneTitle);
});