使用jQuery将列添加到表中

The*_*sta 19 javascript jquery html-table

是否可以将列添加到现有表中,如下所示:

<table id="tutorial" width="600" border="0">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

用js?

Pra*_*ana 32

你可以这样做

 $('#tutorial').find('tr').each(function(){
        $(this).find('td').eq(n).after('<td>new cell added</td>');
   });
Run Code Online (Sandbox Code Playgroud)

n可以替换为要添加新列的列之后的数字

  • 并添加标题,如果你使用它:`$('table').find('tr').each(function(){$(this).find('th').eq(-1). after('<th> HEADER </ th>'); $(this).find('td').eq(-1).after('<td> ROW </ td>');});` (8认同)

Aru*_*hny 16

您可以使用.append()td行追加新内容

$('#tutorial tr').append('<td>new</td>')
Run Code Online (Sandbox Code Playgroud)

演示:小提琴