Jquery 如果没有tr,则隐藏表
我正在输出此表:Jquery 如果没有tr,则隐藏表,jquery,html,Jquery,Html,我正在输出此表: <table class="table-example"> <thead> <tr> <th scope="col">Location</th> <th scope="col">Description</th> <th scope="col">Status</th> <th scope="col">E
<table class="table-example">
<thead>
<tr>
<th scope="col">Location</th>
<th scope="col">Description</th>
<th scope="col">Status</th>
<th scope="col">Eta Fix</th>
</tr>
</thead>
<tbody></tbody>
</table>
我做错了什么 试试这个:
$(函数(){
if($(“.table example>tbody>tr”).length==null | |$(“.table example>tbody>tr”).length==0){
$(“.table example”).hide();
}
});代码>
位置
描述
地位
埃塔修正案
您可以使用.table example>tbody>tr
,但$('.table example').hide()中缺少'代码>。
我希望它能对您起作用。如果表中有tbody标记,选择器$('.table example>tbody')将返回长度为1的值。相反,您需要检查tbody中的任何元素:
$(function(){
if ($('.table-example tbody *').length == 0){
$('.table-example).hide();
}
});
可以将:has
选择器或方法与:empty
选择器一起使用
$('.table example').has('tbody:empty').hide()
位置
描述
地位
埃塔修正案
您要查找的是表格示例中的tbody元素的数量,而不是tr元素。将选择器更改为“.table example>tbody tr”
$(function () {
if ($('.table-example > tbody > tr').length == 0){
$('.table-example').hide();
}
});
$(function(){
if ($('.table-example tbody *').length == 0){
$('.table-example).hide();
}
});