Move containing elements up and down with jquery

后端 未结 2 1758
南旧
南旧 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 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/

提交回复
热议问题