move up/down in jquery

前端 未结 3 1736
醉酒成梦
醉酒成梦 2021-01-07 08:49

I Have 5 spans i am trying to move them up/down (swap positions) in jquery

Up! 
Down!         


        
相关标签:
3条回答
  • 2021-01-07 09:32

    Consider this example. Its not styled up well or anything, but clicking on Up or Down inserts that element either before its previous sibling or after its next sibling.

    <style type="text/css">
        span.swapMe {
            float: left;
            clear: left;
        }
    </style>
    
    <span class="swapMe">Test1 <span class="up">Up!</span> <span class="down">Down!</span></span>
    <span class="swapMe">Test2 <span class="up">Up!</span> <span class="down">Down!</span></span>
    <span class="swapMe">Test3 <span class="up">Up!</span> <span class="down">Down!</span></span>
    <span class="swapMe">Test4 <span class="up">Up!</span> <span class="down">Down!</span></span>
    <span class="swapMe">Test5 <span class="up">Up!</span> <span class="down">Down!</span></span>
    
    
    <script type="text/javascript">
    
    $("span.up").click(function(){
        $(this).parent().insertBefore($(this).parent().prev())
    });
    
    $("span.down").click(function(){
        $(this).parent().insertAfter($(this).parent().next())
    });
    
    
    </script>
    
    0 讨论(0)
  • 2021-01-07 09:34

    You're trying to insert a certain span before the next one, which makes it remain on the same position...

    $("#"+LinkID).insertBefore($("#"+LinkID).prev());
    

    or

    $("#"+LinkID).insertAfter($("#"+LinkID).next()); 
    

    would be better.

    0 讨论(0)
  • 2021-01-07 09:39

    That's because you're telling a span to go before the next span (i.e., stay the same). Try using insertBefore on a previous or insertAfter with a next.

    EDIT Try this on for size: http://jsfiddle.net/eJk3R/

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