Add table row in jQuery

前端 未结 30 2263
既然无缘
既然无缘 2020-11-21 05:40

What is the best method in jQuery to add an additional row to a table as the last row?

Is this acceptable?

$(\'#myTable\').append(\'

        
30条回答
  •  攒了一身酷
    2020-11-21 06:19

    To add a good example on the topic, here is working solution if you need to add a row at specific position.

    The extra row is added after the 5th row, or at the end of the table if there are less then 5 rows.

    var ninja_row = $('#banner_holder').find('tr');
    
    if( $('#my_table tbody tr').length > 5){
        $('#my_table tbody tr').filter(':nth-child(5)').after(ninja_row);
    }else{
        $('#my_table tr:last').after(ninja_row);
    }
    

    I put the content on a ready (hidden) container below the table ..so if you(or the designer) have to change it is not required to edit the JS.

    
    

提交回复
热议问题