如果< li>隐藏父DIV是空的 [英] Hide parent DIV if <li> is Empty

查看:96
本文介绍了如果< li>隐藏父DIV是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这必须非常简单,但是我一直在把头撞在墙上.我的HTML是:

I know this has got to be incredibly simple but I've been banging my head against the wall on this one. My HTML is:

<div class="awardsparent">
<h4>AWARDS</h4>
<ul class="awards"><li></li></ul>
</div>

如果ul.awards li为空,我试图隐藏div.awardsparent容器.这是我的jQuery代码:

I'm trying to hide the container div.awardsparent if the ul.awards li is empty. Here is my jQuery code:

$(function() {          
  if($('ul.awards li:empty')) {
  $('div.awardsparent').hide();
    }
});

谢谢!让我知道是否需要更多信息.

Thank you! Let me know if you need any more info.

推荐答案

从技术上讲,您甚至可能不需要if测试,只需让jQuery的空选择器发挥作用即可.

Technically, you probably don't even need the if test, just let jQuery's empty selectors do the magic.

这只班轮应该做.

$("ul.awards li:empty").closest('div.awardsparent').hide()

并且用空的选择器表示,如果$("ul.awards li:empty")返回某些内容,则它将隐藏父级,但是如果选择为空,则它将无声地忽略closest(),因为没有任何内容将其应用于.

and by empty selector I mean that if $("ul.awards li:empty") returns something then it will hide the parent, but if the selection is empty it will silently ignore the closest() as there is nothing to apply it to.

这篇关于如果&lt; li&gt;隐藏父DIV是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆