如何检查td中是否存在元素

时间:2014-03-04 15:49:05

标签: javascript jquery html css

我正在关注html

<td id="td0" style="width:180px;" class="even"> 
        <strong>Bedroom</strong> <br>
        <span class="manufacturer_links"> 
            <a href="http://testsite.com/bedroom-furniture/beds.htm?manufacturer=2441">Coaster Furniture&nbsp;Beds</a>
        </span>
    <br>    
        <span class="manufacturer_links"> 
        <a href="http://testsite.com/bedroom-furniture/bed-frames-headboards.htm?manufacturer=2441">Coaster Furniture&nbsp;Bed Frames &amp; Headboards</a>
        </span>
    <br>
    <br>            
</td>

我不想显示整个

<td id="td0" style="width:180px;" class="even"> td的class和id将动态更新。

如果td

中不存在<span class="manufacturer_links">

怎么做?

3 个答案:

答案 0 :(得分:2)

使用选择器魔术:

$('td:not(:has(span.manufacturer_links))').addClass('collapsed');

答案 1 :(得分:1)

jQuery的:

$("td#td0").not(":has(span.manufacturer_links)").css("display", "none");

答案 2 :(得分:1)

您可以检查它是否存在,然后执行任何操作:

if( $('#td0 .manufacturer_links').length == 0) { //the class we look for does not exist in td0 element
    //do anything, such as delete the td?
    $('#td0').remove();
}