如何使用 jQuery 删除 html table 中的当前列?
How can I delete current column in an html table using jQuery?
我在 table header 中有一张这样的图片:
<table class="templateTable">
<thead>
<tr>
<th><img src="/Images/delete.png" /></th>
<th><img src="/Images/delete.png" /></th>
</tr>
</thead>
<tbody>
<tr><td></td><td></td></tr>
<tr><td></td><td></td></tr>
<tr><td></td><td></td></tr>
</tbody>
</table>
我希望当我单击图像时它会从 able 中删除当前列。 table 没有 id 属性。这可能吗?
您可以使用基于索引的删除
$('.templateTable thead img').click(function () {
var $this = $(this),
$td = $this.parent(),
index = $td.index() + 1;
$(this).closest('table').find('tr > :nth-child(' + index + ')').remove();
})
演示:Fiddle
我在 table header 中有一张这样的图片:
<table class="templateTable">
<thead>
<tr>
<th><img src="/Images/delete.png" /></th>
<th><img src="/Images/delete.png" /></th>
</tr>
</thead>
<tbody>
<tr><td></td><td></td></tr>
<tr><td></td><td></td></tr>
<tr><td></td><td></td></tr>
</tbody>
</table>
我希望当我单击图像时它会从 able 中删除当前列。 table 没有 id 属性。这可能吗?
您可以使用基于索引的删除
$('.templateTable thead img').click(function () {
var $this = $(this),
$td = $this.parent(),
index = $td.index() + 1;
$(this).closest('table').find('tr > :nth-child(' + index + ')').remove();
})
演示:Fiddle