jQuery:隐藏空表行 [英] Jquery: hiding empty table rows
问题描述
我正在尝试编写一个脚本,该脚本检测表td
是否为空以及它们是否隐藏了父级tr
I am trying to write a script that detects if a tables td
s are empty and if they are hide the parent tr
我已经搜索了Stack Overflow,并找到了其他脚本,但似乎没有一个适合我.
I've searched Stack Overflow and found other scripts but none seem to work for me.
到目前为止,我有:
$(".table tr").each(function() {
var cell = $(this).find('td').html();
if (cell == null){
console.log('empty');
$(this).parent().addClass('nodisplay');
}
});
但是无法正常工作.任何建议,将不胜感激!
but just can't get it working. Any advice would be appreciated!
小提琴: http://jsfiddle.net/MeltingDog/S8CUa/1/
推荐答案
.html()
仅返回第一个匹配元素的内容,因此,如果您的行有多个单元格,则此行将无效. .text()
可能是更好的解决方法,除非您在单元格中有图像或其他空标签. /p>
.html()
only returns the content of the first matched element, so if your rows have more than one cell this wouldn't work. .text()
might be a better fix, unless you have images or other empty tags in the cells.
$("table tr").each(function() {
var cell = $.trim($(this).find('td').text());
if (cell.length == 0){
console.log('empty');
$(this).addClass('nodisplay');
}
});
这篇关于jQuery:隐藏空表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!