tr
1 つの列を追加する場合は、各テーブルで反復処理を行い、新しい列を追加できますtd
例:
$("#id_of_your_table > tbody > tr").each(function(index, trElement) {
// Add a new TD at the end
$(trElement).append($(document.createElement("td")));
});
注: 適切な動作のために を追加することを忘れないtbody
でくださいtable
(ブラウザによっては、存在しない場合にデフォルトで追加されます) 。
例:
<table id="myTable">
<tbody>
<tr>
.....
完全な例を次に示します。
<button id="addColumn">Add new column</button>
<table id="myTable">
<tbody>
<tr>
<td>col 1</td>
<td>col 2</td>
<td>col 3</td>
</tr>
<tr>
<td> col 1</td>
<td> col 2</td>
<td> col 3</td>
</tr>
</tbody>
</table>
<script>
// When DOM is ready
$(document).ready(function() {
// Listen on the click on the button
$("#addColumn").on('click', function(mouseEvent) {
// Iterate on all TR of the table
$("#id_of_your_table > tbody > tr").each(function(index, trElement) {
// Add a new TD at the end
$(trElement).append($(document.createElement("td")));
});
});
});
</script>