gpt4 book ai didi

jQuery 删除 HTML 表格列

转载 作者:行者123 更新时间:2023-12-03 21:38:06 25 4
gpt4 key购买 nike

我有一个像这样的 HTML 表格:

<table border="1">
<tbody>
<tr>
<td><a href="#" class="delete">DELETE ROW</a>COL 1</td>
<td><a href="#" class="delete">DELETE COL</a>COL 2</td>
<td><a href="#" class="delete">DELETE COL</a>COL 3</td>
<td><a href="#" class="delete">DELETE COL</a>COL 4</td>
<td><a href="#" class="delete">DELETE COL</a>COL 5</td>
<td><a href="#" class="delete">DELETE COL</a>COL 6</td>
</tr>
<tr>
<td>ROW 1</td>
<td>ROW 1</td>
<td>ROW 1</td>
<td>ROW 1</td>
<td>ROW 1</td>
<td>ROW 1</td>
</tr>
<tr>
<td>ROW 2</td>
<td>ROW 2</td>
<td>ROW 2</td>
<td>ROW 2</td>
<td>ROW 2</td>
<td>ROW 2</td>
</tr>
</tbody>
</table>

当我单击“删除”类的链接时,我需要一个函数来删除指定的列。你能帮我吗 ?

最佳答案

几年后,可能是时候更新这个问题的答案了。

// Listen for clicks on table originating from .delete element(s)
$("table").on("click", ".delete", function ( event ) {
// Get index of parent TD among its siblings (add one for nth-child)
var ndx = $(this).parent().index() + 1;
// Find all TD elements with the same index
$("td", event.delegateTarget).remove(":nth-child(" + ndx + ")");
});

关于jQuery 删除 HTML 表格列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1068881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com