2017-10-05 55 views
0

要温柔,仍然学习,但是我的代码有什么问题,我需要查看每个容器div,看看某个div是否为空,并删除链接/按钮。jQuery - 隐藏按钮/链接,如果div是空的

$('.eventText').each(function() { 
if($('.exPanel').is(':empty')) { 
     $('a.eventLink').hide(); 
    } 
}); 

FIDDLE

任何帮助,将不胜感激。

感谢

回答

0

有一对夫妇在这里考虑的......

首先是一个元素不是:empty如果它包含在某些浏览器换行符,所以你可能想看看里面。

我也使用上下文来查找当前匹配中的元素,而不是查找所有元素。

$('.eventText').each(function() { 
    console.log($('.exPanel', this)); 

    if(!$.trim($('.exPanel', this).html())) { 
     $('a.eventLink', this).hide(); 
    } 
}); 
+0

感谢您的帮助,这真是棒极了。 – webmonkey237

0

在您的代码中,.exPanel不为空。你应该添加更多的代码使用jQuery的trim()

if ($.trim($('.exPanel').html().length) < 1) { 
    $('a.eventLink').hide(); 
} 

if (!$.trim($('.exPanel').html().length)) { 
    $('a.eventLink').hide(); 
} 
0

您可以使用此

if ($('.exPanel').children().length < 1) { 
    // do something 
} 

children()函数返回一个包含孩子一个jQuery对象修剪空白。所以你只需要检查尺寸,看看它是否少于一个孩子,等于没有孩子

0

试试这个

$('.eventText .exPanel').each(function(){ 
    if (!$(this).text().trim().length) { 
    $(this).siblings('a.eventLink').hide(); 
    } 
});