简体   繁体   English

如何使用jQuery在DOM上移动几个div

[英]How to move several divs up the DOM with jQuery

I would like to move several divs up the DOM with jQuery. 我想使用jQuery将DOM向上移动几个div。

<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. 我想将所有.list div移到.title div之后。 How can I do this with jQuery. 我该如何使用jQuery。

This $('.temps').insertAfter('.title'); 这个$('.temps').insertAfter('.title'); does not work, it places all the .list after the first .title 不起作用,它将所有.list放在第一个.title之后

I would like to move all the .list divs after the .title div. 我想将所有.list div移到.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. 如果没有@Bhojendra Nepal的上述答复,我将无能为力,尽管他的解决方案在代码段中有效,但对我的应用程序却无效。 In the end the below worked for me. 最后,以下内容对我有用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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