I've got a table that looks like this:
<table>
<tr>
<td> </td>
</tr>
</table>
How can a i structure a jQuery selector in such a way that it will look for a table with a cell 开发者_Go百科containing
and set the table to display: none;
Help please?
$('table').each(function ()
{
var $table = $(this),
numTRs = $table.find('tr').length,
$tds = $table.find('td'),
numTDs = $tds.length;
if (numTRs === 1 && numTDs === 1 && $tds.html() === ' ')
{
$table.hide();
}
});
Test cases: http://jsfiddle.net/mattball/WhAcB/
Have a look at the following
$('table').each(function() {
var $this = $(this);
var trCount = $this.find('tr').length;
var tdCount = $this.find('td').length;
if (trCount == 1 && tdCount == 1) {
var tdContents = $this.find('td:first').html();
if (tdContents == ' ') {
setTimeout(function() {
$this.hide();
}, 2000);
}
}
});
This will work according to specs and is still pure Jquery:
$('table tr:only-child td:only-child').filter(function () {
return $(this).html() === ' ';}).closest("table").hide();
What troubled me was the special character in the table. Otherwise it would be simpler.
<table>
<tr>
<td>nbsp</td>
</tr>
</table>
<script type="text/javascript">
$('table tr:only-child td:only-child:contains("nbsp")').closest("table").hide();
</script>
精彩评论