jQuery jquery重新排序div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10088496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:48:53  来源:igfitidea点击:

jquery reorder divs

jqueryhtml

提问by LeBlaireau

I have 3 divs want to reverse the order on doucment ready

我有 3 个 div 想要在准备好文档时颠倒顺序

<div id="block-1">First</div>
<div id="block-2">Second</div>
<div id="block-3">Third</div>

How can I do this in jquery?

我怎样才能在 jquery 中做到这一点?

回答by Chuck Norris

<div id="parent">
<div id="block-1">First</div>
<div id="block-2">Second</div>
<div id="block-3">Third</div>
</div>

And try this in Jquery

在 Jquery 中试试这个

$('#parent > div').each(function() {
    $(this).prependTo(this.parentNode);
});?

You can see example in jsfiddle http://jsfiddle.net/N7PGW/

您可以在 jsfiddle http://jsfiddle.net/N7PGW/ 中看到示例

回答by Fabrizio Calderan

Just use

只需使用

$('#block-2').insertBefore('#block-1');
$('#block-3').insertBefore('#block-2');

Example fiddle: http://jsfiddle.net/2DUXF/

示例小提琴:http: //jsfiddle.net/2DUXF/

回答by Shyju

This will reverse all divs inside a div with id "div1"

这将反转 id 为“div1”的 div 内的所有 div

  $(function(){
    var items=$("#div1 div").toArray();
        items.reverse();
        $.each(items,function(){
           $("#div1").append(this); 
        });     
    });?

Here is the jsFiddle http://jsfiddle.net/bCAVz/8/

这是 jsFiddle http://jsfiddle.net/bCAVz/8/

回答by Explosion Pills

$(
   $("div[id|=block]")
      .slice(1)
      .get()
      .reverse()
 )
    .insertBefore("div[id|=block]:first");

http://jsfiddle.net/8adwS/

http://jsfiddle.net/8adwS/

Also note that you can add the array reverse syntax to the jQuery function syntax, which would save you a selector.

另请注意,您可以将数组反向语法添加到 jQuery 函数语法中,这将为您节省一个选择器。