Javascript JQuery在div没有内容时隐藏父元素
我有这样的代码。。如果div class=content中没有内容,我需要隐藏父div。 谁能告诉我怎么做。 这看起来很简单,谷歌搜索了2-3个小时,但我无法解决这个问题Javascript JQuery在div没有内容时隐藏父元素,javascript,jquery,jquery-ui,Javascript,Jquery,Jquery Ui,我有这样的代码。。如果div class=content中没有内容,我需要隐藏父div。 谁能告诉我怎么做。 这看起来很简单,谷歌搜索了2-3个小时,但我无法解决这个问题 <div id="row-custom_57" class="section custom_57-section"> <div class="label"> Designation </div> <d
<div id="row-custom_57" class="section custom_57-section">
<div class="label">
Designation
</div>
<div class="content">
</div>
<div class="clear"></div>
</div>
<div id="row-birth_date" class="section birth_date-section">
<div class="label">
Date of birth
</div>
<div class="content">
</div>
<div class="clear"></div>
</div>
<div id="row-birth_date" class="section birth_date-section">
<div class="label">
Date of birth
</div>
<div class="content">
</div>
<div class="clear"></div>
</div>
非常感谢这样就可以了:
$('.content').each(function(){
if($(this).html()==''){
$(this).parent().hide();
}
});
从
$(this).html()
返回的值是一个字符串,因此它永远不会是null
,它只会是'
(空)内容实际上是null还是空字符串
如果您尝试以下操作,会发生什么情况:
if($(this).html().match(/^\s*$/))
{
console.log('innnnn' + $(this).html);
}
我认为它更喜欢这样检查:
$(this).text()
因为它只返回不包括html标记的文本。
console.log('innnn'+$(this.html))代码>如果..则不输入它。。我也尝试使用if($(this.html()=''){//do something},但它也不起作用。使用.is(':empty')来确定元素是否为空。为什么人们不能创建JSFIDLE并将其发布到他们的问题中?为什么?第一世界的问题…谢谢你。工作正常。非常感谢,节省了我的时间。谢谢大家宝贵的时间。
$(this).text()