我有这个表格的结构
<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>
每一行都有一个删除按钮,使用AJAX并删除tr元素本身。当所有行都被删除时,可能会发生tbody变为空的情况,此时我希望删除整个表格。
我知道有一个伪选择器
$('#latest tbody:empty')
,但这只选择tbody。当tbody为空或类似情况时,我想选择整个表格。编辑回答后:
我删除所有行后检查了元素:只有thead标签和tbody标签。尽管如此,它仍未删除表格元素。请看代码。
// TR Fading when deleted
$('.delete').live('click', function() {
$.ajax({
type: 'POST',
url: 'history/delete/id/'+$(this).attr('id')
});
$(this).closest('tr').fadeOut('slow', function() {
$(this).remove();
if($(this).closest('table').find('tbody').is(':empty'))
$('#latest').remove();
});
return false;
});
if
语句中使用$(this).closest('table').find('tbody').is(':empty')
。 - jAndy.remove()
和detach()
会移除元素,但是它们会留下一些空格或换行符,所以由于:empty
也检查文本节点,因此它不为空。使用if($('#latest tbody').children().length() < 1)
作为语句! - jAndy