使用jquery查找和隐藏元素
在下面的示例中,如何使用IDs数组检查和隐藏使用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
#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()
}
})
})