Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 清理空表时出现逻辑错误_Javascript_Jquery - Fatal编程技术网

Javascript 清理空表时出现逻辑错误

Javascript 清理空表时出现逻辑错误,javascript,jquery,Javascript,Jquery,目前,我的文档中有许多表都在类table.grid下。我正在尝试遍历并删除任何没有字段的表头,即生成的空表/没有结果的表。表头表格类的继承权为table.grid tbody tr.header th,td随后为table.grid tbody tr td。目前我的代码如下: $('table.grid tbody tr.header th').each(function (i) { var remove = 0; var tds = $(this).parents('tbody

目前,我的文档中有许多表都在类
table.grid
下。我正在尝试遍历并删除任何没有
字段的表头,即生成的空表/没有结果的表。表头表格类的继承权为
table.grid tbody tr.header th
,td随后为
table.grid tbody tr td
。目前我的代码如下:

$('table.grid tbody tr.header th').each(function (i) {
    var remove = 0;
    var tds = $(this).parents('tbody').find('tr td:nth-child(' + (i + 1) + ')');

    tds.each(function (j) {
        if (this.innerHTML == '') remove++;
    });

    if (remove == ($('table.grid tbody tr td').length - 1)) {
        $(this).hide();
        tds.hide();
    }
});

但不幸的是,这会删除每个表头。不仅仅是那些没有内容的人。我相信我用if语句错误地调用了
td
字段,但我不确定如何修复它。我尝试将其命名为
$(this).next('table.grid tbody tr td').length-1)
,并且还尝试了
最近的()
操作符,但没有成功。

我不能100%肯定我理解您隐藏表的确切要求。我假设一个“空”表中只有一行带有标题信息。任何其他行都有内容,使该表非空。我决定利用这一点,检查非标题行的数量:

$('table.grid').each(function (i) {
    //Select all rows in this table that aren't .header
    var rows = $(this).find("tr").not(".header");

    if (rows.length == 0)
    {
        //no rows except for header - hide the table.
        $(this).hide();
    }
});
您可以使用提供的HTML在中看到这一点。它隐藏了第二个表,因为它没有超出标题的行

只是一张纸条。。。如果您控制这个应用程序的服务器端,您绝对最好的策略是检测一个空的结果集并跳过它,这样以后就不必清理它了。您可能没有该选项,因此这可能有助于您在浏览器中处理它


如果我误解了您对隐藏内容和时间的需求,请务必告诉我。

有一种更简单的方法可以决定何时删除行。设置
var remove=true。如果
this.innerHTML!='',则在tds.each()内部将删除设置为false。之后,如果
remove
为true,则隐藏该行。这比把空单元格数加起来并与行中的单元格数进行比较要好得多。您能给我们举一个简单的例子,说明一个表中有一行要删除吗?这会帮助我检查你的选择器。当然。对不起,太不具体了。查看此JSFIDLE以获取两个截面表的示例。第一个表将被保留,因为它有结果,但我希望清除整个空的第二个表,因为在创建文档时没有结果被合成。如果你需要更多的信息或者我不小心漏掉了什么,请告诉我。那真是太好了。不过你说得对,不幸的是我完全无法访问服务器端的任何东西。只是修补了一些令人恶心的视觉混乱:(