gpt4 book ai didi

javascript - 如何使用 jQuery 隐藏空的 html 表格单元格?

转载 作者:行者123 更新时间:2023-11-28 05:09:05 25 4
gpt4 key购买 nike

我有一个 5×7 的 HTML 表格。在许多查询中,填满整个表格的元素少于 35 个。

在这种情况下,如何使用 jQuery(或任何其他有效方式)动态地“隐藏”空单元格?

最佳答案

编辑 - 改进版本

// Grab every row in your table
$('table#yourTable tr').each(function(){
if($(this).children('td:empty').length === $(this).children('td').length){
$(this).remove(); // or $(this).hide();
}
});

未经测试但看起来逻辑合理。

// Grab every row in your table
$('table#yourTable tr').each(function(){
var isEmpty = true;
// Process every column
$(this).children('td').each(function(){
// If data is present inside of a given column let the row know
if($.trim($(this).html()) !== '') {
isEmpty = false;
// We stop after proving that at least one column in a row has data
return false;
}
});
// If the whole row is empty remove it from the dom
if(isEmpty) $(this).remove();
});

关于javascript - 如何使用 jQuery 隐藏空的 html 表格单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2632008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com