【发布时间】:2011-05-31 09:34:58
【问题描述】:
我有一堆表格行,例如:
<tr>
<td>cell1</td>
<td>cell2</td>
<td><a href="action.php">cell3</a></td>
</tr>
当有人点击 cell3 中的链接时,有没有办法隐藏整个 tr 行?那么当他们点击 cell3 中的链接时,整个 tr 都被隐藏了?
【问题讨论】:
标签: jquery html-table hide
我有一堆表格行,例如:
<tr>
<td>cell1</td>
<td>cell2</td>
<td><a href="action.php">cell3</a></td>
</tr>
当有人点击 cell3 中的链接时,有没有办法隐藏整个 tr 行?那么当他们点击 cell3 中的链接时,整个 tr 都被隐藏了?
【问题讨论】:
标签: jquery html-table hide
这是.delegate()的好地方,像这样:
$("#tableID").delegate("td:nth-child(3)", "click", function() {
$(this).closest("tr").hide();
});
通过使用.delegate(),我们将一个 click 处理程序附加到所有第三列单元格的<table>,然后使用.closest() 爬到<tr> 到.hide()。如果你想要它在链接上,只需将td:nth-child(3)更改为td a,其余相同。
【讨论】:
querySelectorAll 方法。当不可用时,它会模仿其行为。都是javascript。
只需简单地使用 jQuery 并隐藏父级。
$('td.hide_on_click').live('click', function(){
// PICK ONE OF THESE:
$(this).parent('tr').remove(); //completely destroy the tr (remove from DOM)
$(this).parent('tr').hide(); //just hide it from the user
});
删除 a 标签。如果您希望它具有“链接外观”,请为可点击的内容添加 css 样式:
.clickable {
cursor: pointer;
cursor: hand;
}
然后:
<table>
<tr>
<td></td>
<td></td>
<td class="hide_on_click clickable">delete</td>
</tr>
</table>
【讨论】:
你好,这是我的解决方案
$(document).on("click", "td:nth-child(3)", function () {
if (confirm("Are you sure ?")) {
$(this).closest("tr").hide();
}
});
【讨论】:
是的
$('td a').click(function() {
$(this).parent().parent().hide();
});
【讨论】:
<tr> :)
parent('selector') 的行为与它实际(显然)的行为方式略有不同。
When someone clicks the link in cell3 is there a way to hide the whole tr row?。而且我的解决方案只展示了预期的效果,提问者显然简化了 HTML,因此我提供了一个匹配的简化答案。