Check if an element is empty after using .append
I want to check if a table body (tbody
) is empty using jQuery. So far I've tried something like:
$("#searchTable tbody").is(":empty")
But it doesn't work. Any other ideas?
HTML Sample
<table id="searchTable" cellpadding="0" cellspacing="0" width="100%">
<thead>
<tr>
<th>No.</th>
<th><a href="#">Status</a></th>
<th><a href="#" class="asc">Category</a></th开发者_如何学Python>
<th><a href="#" class="desc">Title</a></th>
<th><a href="#" class="desc">Last Used</a></th>
<th><a href="#" class="desc">URL</a></th>
<th style="width: 96px;">Actions</th>
</tr>
</thead>
<tbody></tbody>
</table>
So I want to check if something was appended in tbody; if not, then alert something.
You could try something like
$('#searchTable tbody').children().length;
Which will be 0 if tbody is empty (has no children).
If the element is not found, it will return false too. Check is the element is present and is selectable using your selector.
精彩评论