简体   繁体   中英

Add Column to table with jQuery

Is it possible to add a column to an existing table like this:

<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>

with js?

you can do like this

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

n can be replaced by the number after which column you want to add new column

You can use .append() to append a new td to the rows

$('#tutorial tr').append('<td>new</td>')

Demo: Fiddle

You mean column not row?

$('#tutorial tr').each(function()
{
    $(this).append('<td></td>');
});

Which selects the <tr> element inside id "tutorial" (that is your table is this case) and append new contents behind its original contents

An alternative option to those above is to create the column together with the other and a style of display:none; and then using the method .Show() to display.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM