jQuery删除表列
问题描述:
我有一张桌子,不能更改标记:
I have a table and cannot change markup:
<table>
<thead>
<tr>
<th>
blablabla
</th>
<th>
blablabla
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
efgd
</td>
<td>
efghdh
</td>
</tr>
</tbody>
</table>
这是我的函数,应删除一列.点击单元格即可调用它:
Here is my function, which should delete a column. It is called on cell click:
function (menuItem, menu) {
var colnum = $(this).prevAll("td").length;
$(this).closest('table').find('thead tr th:eq('+colnum+')').remove();
$(this).closest("table").find('tbody tr td:eq('+colnum+')').remove();
return;
}
但是它将删除其他内容,而不是我要删除的列.我在哪里错了?
But it deletes something else, not the column I wanted to delete. Where am I wrong?
答
列几乎只是单元格,因此您需要手动循环遍历所有行并通过索引删除单元格.
A column is pretty much just cells, so you'll need to manually loop through all the rows and remove the cell by the index.
这应该为删除第3列提供一个良好的起点:
This should give you a good starting point for removing the 3rd column:
$("tr").each(function() {
$(this).filter("td:eq(2)").remove();
});