删除/隐藏表的空列,包括<th>

DGT*_*DGT 13 javascript jquery

如何隐藏包含该列中所有空单元格的<th>列,同时保留其他列及其标题.以下jquery隐藏了整个<th>,这不是我想要的.是一个示例,我想隐藏整个'Column3',包括<th>.提前谢谢了.

$('table#mytable tr').each(function() {
    if ($(this).children('td:empty').length === $(this).children('td').length) {
        $(this).hide();
    }
});
Run Code Online (Sandbox Code Playgroud)

Ste*_*her 15

花了一段时间拼凑起来.感谢nxt的一些代码.

$('#mytable th').each(function(i) {
    var remove = 0;

    var tds = $(this).parents('table').find('tr td:nth-child(' + (i + 1) + ')')
    tds.each(function(j) { if (this.innerHTML == '') remove++; });

    if (remove == ($('#mytable tr').length - 1)) {
        $(this).hide();
        tds.hide();
    }
});
Run Code Online (Sandbox Code Playgroud)


nxt*_*nxt 6

如果要隐藏列,如果所有单元格(忽略标题)都为空,则可以执行以下操作:

$('#mytable tr th').each(function(i) {
     //select all tds in this column
     var tds = $(this).parents('table')
              .find('tr td:nth-child(' + (i + 1) + ')');
        //check if all the cells in this column are empty
        if(tds.length == tds.filter(':empty').length) { 
            //hide header
            $(this).hide();
            //hide cells
            tds.hide();
        } 
}); 
Run Code Online (Sandbox Code Playgroud)

示例:http://jsfiddle.net/DeQHs/

示例2(适用于jQuery> 1.7):http://jsfiddle.net/mkginfo/mhgtmc05/