当子div(即post-title)为空时,我需要隐藏父div(即.post)。但是,如果它包含内容,我不想隐藏其他子div。
这就是我的目的:
<div id="latest">
<div class="post">
<div class="post-content">
<div class="post-title"></div>
</div>
</div>
<div class="post">
<div class="post-content">
<div class="post-title"><h2>Hello!</h2></div>
</div>
</div>
</div>
$(".post-title:empty").parent($(".post")).hide();
答案 0 :(得分:3)
使用closest()
获取与帖子类
$(".post-title:empty").closest('.post').hide();
$(".post-title:empty").closest('.post').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="latest">
<div class="post">
<div class="post-content">
<div class="post-title"></div>
<p>content</p>
</div>
</div>
<div class="post">
<div class="post-content">
<div class="post-title">
<h2>Hello!</h2>
</div>
<p>content hello</p>
</div>
</div>
</div>