我的结构如下:-
<div class="container">
<h1 class="header-div">Title 1</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<div class="image-div">image1</div>
</div>
<div class="container">
<h1 class="header-div">Title 2</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<div class="image-div">image2</div>
</div>
<div class="container">
<h1 class="header-div">Title 3</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<div class="image-div">image3</div>
</div>
我想获取 h1 并将其附加在 .image-div 之后,并希望对每个 block 执行此操作
最佳答案
两件事
appendTo
将追加内部div
而不是之后div
。- 无需迭代,它会将所有这些克隆一起克隆,并为每个
image-div
附加所有这些克隆
你需要尝试
$(".header-div").each( function(){
$(this).clone().insertAfter( $(this).parent().find( ".image-div" ) );
});
或
$(".header-div").each( function(){
$(this).clone().appendTo( $(this).parent() );
});
关于javascript - 克隆第一个 div 并使用类名追加到下一个 div 之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43409582/