Numbering dynamic table rows

前端 未结 3 989
失恋的感觉
失恋的感觉 2021-01-28 18:50

I\'m working on making a dynamic HTML table using jQuery. In a table, my user has two interactions:

  1. Append a row
  2. Remove a specific row

The

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-28 19:34

    http://jsfiddle.net/mblase75/LNXae/1/

    First, wrap the counter number in a with a class for easy finding later:

    $new_row.children('td').prepend('Row #' + ($new_row.index() + 1) + "");
    

    Then update all these spans with an .each loop after you remove the desired row. The first argument passed into .each's callback function is a zero-based index number, and the second is the HTML element:

        var $row = $(this).closest('tr'),
            $table = $row.closest('table');
        $row.remove();
    
        $table.find('tr').each(function(i, v) {
            $(v).find('span.num').text(i + 1);
        });
    

提交回复
热议问题