我想克隆一个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
并相应地附加它们。
对此的任何指导将不胜感激。
提前致谢
答案 0 :(得分:1)
如果我明白你的观点你需要使用.each()
和$(this)
$('.tr_class').each(function(){
var cloneTitle = $(this).find('.section_title').clone();
$(this).next('.append_class').html(cloneTitle);
});