[英]clone first div and append after next div using class name
I am having structure like:- 我有这样的结构: -
<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>
I want to take h1 and append it after .image-div and want to do it for every block 我想取h1并将其追加到.image-div之后,并希望为每个块执行此操作
Two things 两件事情
appendTo
will append inside div
not after div
. appendTo
将在 div
之后追加div
。 image-div
没有迭代它会将所有这些克隆在一起并为每个image-div
附加所有这些克隆 You need to try 你需要试试
$(".header-div").each( function(){
$(this).clone().insertAfter( $(this).parent().find( ".image-div" ) );
});
or 要么
$(".header-div").each( function(){
$(this).clone().appendTo( $(this).parent() );
});
It could be done like so 它可以这样做
var container = $(".container");
$.each(container, function(k, e){
$(e).find('.header-div').clone().appendTo($(e).find(".image-div"))
} )
each
loops through array of main div elements where image and title divs are, and just move title to another DOM position. each
循环遍历图像和标题div所在的主div元素数组,并将标题移动到另一个DOM位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.