繁体   English   中英

如果没有tr,请隐藏表格

[英]If no tr, hide table

我正在输出这个表:

<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();
  }
});

我究竟做错了什么?

可以使用:has选择器或方法以及: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> 

尝试这个:

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

$(function () {

  if ($('.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();
  }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM