Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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/batch-file/6.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
使用jquery查找和隐藏元素_Jquery_Each - Fatal编程技术网

使用jquery查找和隐藏元素

使用jquery查找和隐藏元素,jquery,each,Jquery,Each,在下面的示例中,如何使用IDs数组检查和隐藏#volume2 <tr id="mytr"> <th id="volume1">1</th> <th id="volume2">2</th> <th id="volume3">3</th> </tr> <tr> <th class="volume1">1</th> <th class="volum

在下面的示例中,如何使用IDs数组检查和隐藏
#volume2

<tr id="mytr">

 <th id="volume1">1</th>
 <th id="volume2">2</th>
 <th id="volume3">3</th>

</tr>

<tr>

 <th class="volume1">1</th>
 <th class="volume2"></th> <-- this is empty
 <th class="volume3">3</th>

</tr>

var IDs = [];
$("tr#mytr").find("th").each(function(){ IDs.push(this.id); }); 

1.
2.
3.
1.

试试这样的

$("tr").find("th").each(function(){
    if( $('.' + this.id).length < 1 ) {
        $(this).addClass('hide');
    }
});
$(“tr”).find(“th”).each(函数(){
if($('..+this.id).length<1){
$(this.addClass('hide');
}
});

您可以计算该类中存在多少非空元素。如果该类中只有emepty元素(没有非空元素),请添加“hide”类:

或者,您可以显式测试单元格是否包含内容:

return $('.' + this.id).filter(function() { 
    return $.trim($(this).text()) !==  '';
}).length === 0;

这也会认为只有空白字符的单元格是空的。

尝试

jQuery(function () {
    $('table tr:nth-child(2) th').each(function () {
        if (!$.trim($(this).text())) {
            $('#' + this.className).hide();
            $(this).hide()
        }
    })
})

演示:

谢谢。如果
根本不存在呢?我仍然可以使用.length==0,对吗?
return $('.' + this.id).filter(function() { 
    return $.trim($(this).text()) !==  '';
}).length === 0;
jQuery(function () {
    $('table tr:nth-child(2) th').each(function () {
        if (!$.trim($(this).text())) {
            $('#' + this.className).hide();
            $(this).hide()
        }
    })
})