[英]How to hide all tr from table except clicked one
我想隐藏表中的所有<tr>
,除了我点击的那些。
<table>
<tr>
<td>ABC</td>
<td>DEF</td>
<td><i class="delete">delete </i></td>
</tr>
<tr>
<td>ABC</td>
<td>DEF</td>
<td><i class="delete">delete </i></td>
</tr>
</table>
单击删除按钮会隐藏除当前之外的所有行。
Jquery代码: -
$(document).ready(function () {
$('table tbody tr').siblings().not($(this)).hide();
});
请建议我。
使用siblings
如下:
请参阅代码中的内联注释:
// Bind click event on `tr` inside `table`
$('table').on('click', 'tr', function () {
// Show current `tr` and hide others
$(this).siblings().hide();
});
您需要在单击事件处理程序下运行代码,而不是在页面加载时运行。 另请注意, .not(this)
在使用siblings()
时是多余的,因为无论如何都不会包含原始元素。 尝试这个:
$(document).ready(function() {
$('table tbody tr').click(function() {
$(this).siblings().hide();
});
});
$(document).ready(function () {
$('table tbody tr').click(function(){
$('table tbody tr').hide();
$(this).show();
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.