如果为空(隐藏),则隐藏HTML / JQuery表

时间:2016-02-21 17:44:55

标签: php jquery html css

以下是我网站上的源代码,它显示了一个没有数据填充它的PHP文件生成的空表,如果表是空的,我该如何隐藏这些表并从页面中删除代码。

<form class="cart" method="post" enctype='multipart/form-data'>
<table class="footable" cellspacing="0" class="group_table">
    <thead>
<tr id="price-table-custom-header">
        <th><center>head1</center></th>
        <th><center>Information</center></th>
        <th data-sort-initial="true"><center>head3</center></th>
        <th><center>Purchase</center></th>
</tr>
    </thead>  
<tbody> </tbody>
<tfoot class="hide-if-no-paging">
<tr>
<td colspan="4">
<div class="pagination pagination-centered">
<ul></ul>
</div>
</td>
</tr>
</tfoot>
</table>

2 个答案:

答案 0 :(得分:1)

您可以验证tbody标记是否为空,如果是,则隐藏整个表。

$(document).ready(function(){
   if ($.trim($(".group_table tbody").text()).length == 0) {
     $('.group_table').hide();
   }                                           
});

编辑:

你的表有两个类属性,要添加两个类,你应该这样做:

<table cellspacing="0" class="group_table footable">

Js Fiddle:

https://jsfiddle.net/jsb3z0y4/

答案 1 :(得分:0)

为什么不检查它是否有任何内容,如果没有,则隐藏它。

var hasContent = $('.group_table tbody').text().trim();

if(!hasContent){
    $('.group_table tbody').hide();
}