Swapping rows in JQuery

前端 未结 10 1598
后悔当初
后悔当初 2020-12-08 19:58

If I have a table as shown below, and have a up and down arrow that moves rows up and down, how would I go about swapping rows in JQuery?



        
10条回答
  •  暖寄归人
    2020-12-08 20:20

    Here's a slightly expanded example, hoping you will find it useful... :)

    $('table').on('click', '.move-up', function () {
        var thisRow = $(this).closest('tr');
        var prevRow = thisRow.prev();
        if (prevRow.length) {
            prevRow.before(thisRow);
        }
    });
    
    $('table').on('click', '.move-down', function () {
        var thisRow = $(this).closest('tr');
        var nextRow = thisRow.next();
        if (nextRow.length) {
            nextRow.after(thisRow);
        }
    });
    

提交回复
热议问题