Move containing elements up and down with jquery

后端 未结 2 1751
南旧
南旧 2021-01-02 00:11

Thanks for any help you can provide! Currently, I use a ui-sortable code to allow users to move items up and down in order. Now, I want to give each of these items a set of

相关标签:
2条回答
  • 2021-01-02 00:47

    You could try replacing your JS with something like this:

    $(".down").click(function () {
        var $parent = $(this).parents(".leg");
        $parent.insertAfter($parent.next()); 
    });
    
    $(".up").click(function () {
        var $parent = $(this).parents(".leg");
        $parent.insertBefore($parent.prev()); 
    });
    

    http://jsfiddle.net/vexw5/7/

    This is just the basics. There are no animations or anything.

    0 讨论(0)
  • 2021-01-02 01:05

    There are several problems to address in your code so let's go through them in order.

    First there is $('up-button') it is missing the . so it won't select the buttons.

    Next you are using the parent() method which only goes up one level, use parents('.leg') instead.

    insertBefore() is a method that accepts a target as to where to place the content you selected.

    previous() isn't a function, it is prev() instead and it doesn't need a parameter as it just selects the previous element.

    If you combine all of those fixes you would get something like this

    $('.up-button').click(function(){
      $(this).parents('.leg').insertBefore($(this).parents('.leg').prev());
    });
    
    $('.down-button').click(function(){
      $(this).parents('.leg').insertAfter($(this).parents('.leg').next());
    });
    

    As demonstrated on this edited fiddle http://jsfiddle.net/vexw5/6/

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