Swapping rows in JQuery

前端 未结 10 1621
后悔当初
后悔当初 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:27

    Here is the code to swap the rows. Lets take #Row1 and #Row3

    $('#Row1').replaceWith($('#Row3').after($('#Row1').clone(true)));
    

    The clone(true) is used so that events are also taken into account.

    If you want to move row up and down then use this code. To move row UP

    var tableRow = $("#Row1");
    tableRow.insertBefore(tableRow.prev());
    

    To move row DOWN

    var tableRow = $("#Row1");
    tableRow.insertAfter(tableRow.next());
    

提交回复
热议问题