如果所有tds都为空,则隐藏表行
没有每个循环,有没有一种简单的方法可以做到这一点。
我想要隐藏整个
是它的所有
都是空白的。 该表是动态生成的,因此空白行可以是任何位置。
$('#table1 tr:has(td):empty').remove();
HTML
a b c ... a b c
如果你想使用每个,你可以这样做:
$('#table1 tr').each(function() { if ($(this).find('td:empty').length) $(this).remove(); });
jsFiddle示例
我建议,虽然这仍然隐含地检查每个tr
元素:
$('tr').filter( function(){ return $(this).find('td').length == $(this).find('td:empty').length; }).hide();
JS小提琴演示 。
在CSS中,使用:not()
和:empty
伪选择器,这有很多跨浏览器的困难:
tr td:empty { visibility: hidden; height: 0; } td:not(:empty) ~ td:empty { visibility: visible; height: 100%; }
JS小提琴演示 。
参考文献:
- jQuery的:
-
:empty
(jQuery)选择器 。
-
filter()
。
-
find()
。
- CSS:
-
:empty
。
- 一般兄弟
~
组合子 。
-
:not()
。