Javascript 在tbody中没有tr元素时删除表元素
我有这个桌子结构Javascript 在tbody中没有tr元素时删除表元素,javascript,jquery,Javascript,Jquery,我有这个桌子结构 <table id="latest" class="debt"> <thead> <tr> <th width="50">Credor</th> <th width="50">Devedor</th> <th>Motivo</th> <th width="80">Valor<
<table id="latest" class="debt">
<thead>
<tr>
<th width="50">Credor</th>
<th width="50">Devedor</th>
<th>Motivo</th>
<th width="80">Valor</th>
<th width="10"></th>
</tr>
</thead>
<tbody>
<?php foreach ($this->latest as $latest) { ?>
<tr <?php if ($latest->reg_value < 0) { echo "class='error'"; } ?>>
<td><?php echo $latest->findParentRow('Application_Model_DbTable_Names','Creditor')->nam_name; ?></td>
<td><?php echo $latest->findParentRow('Application_Model_DbTable_Names','Debtor')->nam_name; ?></td>
<td><?php echo $latest->reg_reason; ?></td>
<td>R$ <?php echo number_format(abs($latest->reg_value), 2, ',', ' ')?></td>
<td><a href="#" id="<?php echo $latest->reg_id; ?>" class="delete"><img src="http://192.168.0.102/libraries/css/blueprint/plugins/buttons/icons/cross.png" alt=""/></a></td>
</tr>
<?php } ?>
</tbody>
</table>
你可以把它压缩成
$('#latest tbody:empty').parent().remove();
更新
这行不通。看起来remove()
和detach()
在移除tr
元素后确实会留下某种crud。它类似于空白或换行符,因此由于:empty
选择器也会检查文本节点,因此它根本不是空的
所以
if($('#最新tbody').children().length()<1)
$(“#最新”).remove();
我们应该做到这一点
参考资料:如何:
$('#latest tbody:empty').parent();
我无法检查元素是否为空,因为它永远不会为空,因为文本节点仍保留在其标记中。我必须检查
是否包含
。删除所有行后,我检查了元素:标记中只有和thead标记。不过,它并没有删除table元素。看看我的代码thx@rasouza:从您的代码中,您可能应该使用$(this).最近('table').find('tbody').is(':empty')
作为if语句的if语句代码为什么不适用于这种情况?为什么我必须使用find()?谢谢你的无知,但它仍然不起作用。看看现在的情况:哇。。对,我测试了一下。看起来像是.remove()
和detach()
删除元素,但它们会留下某种空白或特征线,因此既然:empty
也会检查文本节点,那么它就不是空的。使用if($('#latest tbody').children().length()<1)
as语句!
$('#latest tbody:empty').parent().remove();
if($('#latest tbody').children().length() < 1)
$('#latest').remove();
$('#latest tbody:empty').parent();
$("#latest:has('tbody:empty')").remove();
if ($('#latest tbody').children().length==0) {
$('#latest').remove();
};