隐藏父 div 如果 :空



如果文档准备好时同级为空,则尝试隐藏父div。似乎对我不起作用:

$('.pp-post-content-location:empty').parent().hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<span class="pp-post-content-subtitle">Location</span>
<div class="pp-post-content-location"></div>
</div>

首先,您尚未在脚本中包含文档 .ready

这是代码 试试这个

$(document).ready(function(){
var text=$('.pp-post-content-location').text(); //text Is Jquery Function which gets the content inside a element
if(text==""){  
$('.pp-post-content-location').parent().hide();
}
});

然后只是一个简单的 if 条件来检查指定元素中是否有任何内容如果它没有任何内容,那么它将隐藏 PARENT 元素

最新更新