简体   繁体   中英

How to move several divs up the DOM with jQuery

I would like to move several divs up the DOM with jQuery.

<section>
    <div class="title"></div>
    <div class="temps"></div>
    <div class="list"></div>
</section>

I would like to move all the .list divs after the .title div. How can I do this with jQuery.

This $('.temps').insertAfter('.title'); does not work, it places all the .list after the first .title

I would like to move all the .list divs after the .title div.

You can use each method like this:

  $('.list').each(function(){ $(this).insertAfter( $(this) //.list div .closest('section') //find parent div section .find('.title') //find child div ); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <section> <div class="title">title</div> <div class="temps">temps</div> <div class="list">list</div> </section> <section> <div class="title">title</div> <div class="temps">temps</div> <div class="list">list</div> </section> <section> <div class="title">title</div> <div class="temps">temps</div> <div class="list">list</div> </section> 

simply try this, demo

$('.list').insertAfter($('.title'));

or

$('.list').insertAfter('.title'); //only selector no object

Try this

$('.list').each(function(){
   $(this).prevAll('.title').after($(this));
});

I wouldn't of been anble to get this working without @Bhojendra Nepal help in his answer above, and while his solution worked in his code snippit it did not work on my application. In the end the below worked for me.

jQuery('.list').each(function(){
   jQuery(this).insertAfter(jQuery(this).parent().find('.title'));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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