我有一个表,无法更改标记:
<table>
<thead>
<tr>
<th>
blablabla
</th>
<th>
blablabla
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
efgd
</td>
<td>
efghdh
</td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
这是我的函数,它应删除一列.在单击单击时调用它:
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;
}
Run Code Online (Sandbox Code Playgroud)
但它会删除其他内容,而不是我想要删除的列.我哪里错了?
Ari*_*iel 14
列几乎只是单元格,因此您需要手动遍历所有行并通过索引删除单元格.
这应该为您提供了删除第3列的良好起点:
$("tr").each(function() {
$(this).filter("td:eq(2)").remove();
});
Run Code Online (Sandbox Code Playgroud)
这.delegate()用于处理程序,以及一种更原生的方法,cellIndex用于获取单击的单元格索引,并cells从每一行拉出单元格.
示例: http ://jsfiddle.net/zZDKg/1/
$('table').delegate('td,th', 'click', function() {
var index = this.cellIndex;
$(this).closest('table').find('tr').each(function() {
this.removeChild(this.cells[ index ]);
});
});
Run Code Online (Sandbox Code Playgroud)
小智 6
这对我来说很好:
$(".tableClassName tbody tr").each(function() {
$(this).find("td:eq(3)").remove();
});
Run Code Online (Sandbox Code Playgroud)
如果你有静态 html(考虑有 10 列的表格),
然后使用以下命令删除第一列和标题:
$('#table th:nth-child(1),#table td:nth-child(1)').remove();
Run Code Online (Sandbox Code Playgroud)
现在新表将有 9 列,现在您可以使用数字删除任何列:
$('#table th:nth-child(7),#table td:nth-child(7)').remove();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24750 次 |
| 最近记录: |