2009-06-15 100 views
0

,如果我有这样的表我该如何隐藏与他们与jQuery的特定单词td元素?

<table width="500" border="0" style="border:0px;" id="options"> 
<tr> 
<td>Designers</td> 
<td><input type="checkbox"> 
</tr> 
</table> 

如何将我隐藏与设计师行?

我是想这将是这个样子

$(document).ready(function() { 
    if($('table #options tr td').html('Designers') { 
    $(this).css('display','none'); 
    } 
    }); 

但我不知道

感谢

回答

7

这应该这样做,假设当你说“行”你的意思是<tr>,不是<td>

$(document).ready(function() { 
    $('td', '#options').filter(function() { // select all the TDs 
     return $(this).text() == 'Designers'; // keep the ones that have 
               // 'Designers' as their HTML 
    }).each(function() { // loop through each of the ones that matched 
     $(this).closest('tr').hide(); // find the parent tr and hide it 
    }); 
}); 

如果举ST想隐藏实际<td>(这不是一排,而是一个单元格),那么你可以这样做:

$(document).ready(function() { 
    $('td', '#options').filter(function() { // select all the TDs 
     return $(this).text() == 'Designers'; // keep the ones that have 
               // 'Designers' as their HTML 
    }).hide(); 
}); 

隐藏表格单元格是值得怀疑的味道,不过...

+0

一确定内容的更好方法是使用$(this).text()而不是$(this).html()。这样,如果td元素包含“D esigners”,该元素仍然匹配。在我看来,这是正确的事情。 – kizzx2 2009-06-15 20:47:42

+0

这是一个公平的观点。编辑。 – 2009-06-15 20:54:19

5
$("td:contains('Designers')").hide();