2017-06-27 3 views
1

を隠す:なしTRであれば、私はこのテーブルを出力していますテーブル

を:私はしないようにテーブル全体を必要とする

<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> 

上でこの上のバリエーションを使用してみましたとのtbodyが空の場合に表示

$(function(){ 
    if ($('.table-example > tbody').length == 0){ 
    $('.table-example).hide(); 
    } 
}); 

私は間違っていますか?

+0

とともに:hasセレクタやメソッドを使用することができますtr要素の代わりに.table-exampleの内部にあります。セレクタを ".table-example> tbody tr"に変更してください – SidTheBeard

答えて

1

これを試してみてください:

$(function(){ 
 
    if ($(".table-example > tbody > tr").length == null || $(".table-example > tbody > tr").length == 0){ 
 
    $(".table-example").hide(); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<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>

+0

ありがとうございました:) – Fento

0
$(function() { 

    if ($('.table-example > tbody > tr').length == 0){ 
    $('.table-example').hide(); 
    } 

}); 

あなたは.table-example > tbody > trを使用することができますし、$('.table-example').hide();に「欠けています。 私はそれがあなたのために働くことを願っています。

0

セレクタ$( '。table-example> tbody')は、テーブルにtbodyタグがある場合、長さ1を返します。代わりに、TBODY内の任意の要素をチェックする必要があります。

$(function(){ 
    if ($('.table-example tbody *').length == 0){ 
    $('.table-example).hide(); 
    } 
}); 
2

があるあなたは、TBODY要素の数を探して:emptyセレクタ

$('.table-example').has('tbody:empty').hide()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<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>

関連する問題