简体   繁体   English

克隆第一个div并使用类名追加到下一个div之后

[英]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
  • without iteration it will clone all of them together and append all these clones for every 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() );
});

Fiddle 小提琴

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM