如果div里面没有一个表格,如何隐藏它

时间:2011-12-22 15:57:28

标签: jquery html hide

我有div这样:

<div class="EventsRollup">
    <span class="EventsRollupTitle">Health Lecture Events</span>
    <!--this is where a table would be dynamically inserted by sharepoint 
        based on some filter, if filter is true, a tabel will get in there, 
        else not-->
</div>

使用jQuery,如果没有插入div,如何隐藏整个table因为div具有背景颜色而空白背景颜色显示没有table内容?

3 个答案:

答案 0 :(得分:3)

if ($('.EventsRollup').find('table').length === 0) {
    $('.EventsRollup').hide();
}

这假设只有一个.EventsRollup ...如果还有更多,你可以使用循环......

$('.EventsRollup').each(function() {
    $this = $(this);
    if ($this.find('table').length === 0) {
        $this.hide();
    }
});

答案 1 :(得分:3)

$('.EventsRollup').not(':has(table)').hide();

$('.EventsRollup:not(:has(table))').hide();

答案 2 :(得分:2)

jQuery( ".EventsRollup").filter( function(){
return !this.getElementsByTagName("table").length;
}).hide();