jquery reorder divs

前端 未结 4 579
旧巷少年郎
旧巷少年郎 2021-01-04 20:59

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

First
Second
相关标签:
4条回答
  • 2021-01-04 21:37
    <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

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

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

    0 讨论(0)
  • 2021-01-04 21:39
    $(
       $("div[id|=block]")
          .slice(1)
          .get()
          .reverse()
     )
        .insertBefore("div[id|=block]:first");
    

    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.

    0 讨论(0)
  • 2021-01-04 21:40

    Just use

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

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

    0 讨论(0)
  • 2021-01-04 21:49

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

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

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

    0 讨论(0)
提交回复
热议问题