【发布时间】:2010-01-29 21:24:58
【问题描述】:
我有一个html表格如下
<table>
<tbody>
<tr>
<td>test content</td>
<td><input type="button" onClick="remove()"></td>
</tr>
....
...
</tbody>
</table>
现在,如果相同的模式继续存在,如果单击该行上的删除按钮,我想删除该行。我如何使用 jQuery 实现相同的功能?
【问题讨论】:
我有一个html表格如下
<table>
<tbody>
<tr>
<td>test content</td>
<td><input type="button" onClick="remove()"></td>
</tr>
....
...
</tbody>
</table>
现在,如果相同的模式继续存在,如果单击该行上的删除按钮,我想删除该行。我如何使用 jQuery 实现相同的功能?
【问题讨论】:
更好:
$(this).closest('tr').remove();
<input type="button" onClick="$(this).closest('tr').remove();">
这样做的好处是无论您的 HTML 在单元格中是什么样子都可以正常工作。
【讨论】:
试试这个:
<input type="button" onClick="$(this).parent().parent().remove();">
或者您可以像这样使其更通用:
<script>
$(document).ready(function()
{
$(".btn").click(function(){
$(this).parent().parent().remove();
});
});
</script>
<tr>
<td><input type="button" class="btn"></td>
</tr>
【讨论】: