我如何使用jQuery淡出所有其他tr标签
我希望用户点击复选框,其他tr标签淡出并消失…
这是表的代码
Date Location September 14, 2010 Royal Festival Hall - London, United Kingdom September 15, 2010 O2 Academy Newcastle - Newcastle Upon Tyne, United Kingdom September 16, 2010 Glasgow Barrowlands - Glasgow, United Kingdom
不是一个优雅的解决方案,但这有效
$(function() { $('input:checkbox').click(function() { $(this).closest('tr').siblings().each(function() { $(this).fadeOut(); }); }); });
以下是你取消隐藏它的方式:( 演示 )
$('input:checkbox').click(function() { var check = $(this).attr('checked'); $(this).closest('tr').siblings().each(function() { if (check) { $(this).fadeOut(); } else { $(this).fadeIn(); } }); });
尝试这样的事情:
function clickEvent(e) { $('#the-table tr').not($(e.target).closest('tr')).fadeOut(); }
这基本上是“表中所有的tr
标签,除了点击的复选框的第一个祖先是tr
”。 换句话说,所有其他的。 将此function绑定到复选框的click
事件,您应该很高兴。 另一种表达方式是:
$(e.target).closest('tr').siblings().fadeOut();
我更喜欢第二种方式,但第一种方式更直观正确的IMO。
但请注意,您可能无法在多个浏览器中设置tr
元素的动画效果……您永远不会知道在IE中使用表格会得到什么。
像这样的东西:
$('tr input[type=checkbox]').click(function() { $('tr').not($(this).closest('tr')).fadeOut('slow'); });
演示JSBin
你可以:
$("input:checkbox").click(function(){ $("tr").not($(this).parent().parent()).fadeOut() });