删除或隐藏div(如果为空) [英] Remove or hide a div if it's empty

查看:99
本文介绍了删除或隐藏div(如果为空)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这应该很简单,但无法弄清楚.这是代码.

I know this should be simple but can't figure it out. Here's the code.

<div class="cols lmenu_item1" id="leftMenuWrapper">
<div id="leftmenu"></div>
</div>

如果"leftmenu"为空,我只需要删除"leftMenuWrapper".这就是我一直在使用的东西.

I simply need to remove the "leftMenuWrapper" if "leftmenu" is empty. Here's what I've been using.

$('#leftmenu').empty().remove('#leftMenuWrapper');

很抱歉,如果这是一个简单的问题.有一个星期一!

Sorry if this is a simple question. Having a Monday!

谢谢!

推荐答案

您可以这样做:

$('#leftmenu:empty').parent().remove();

仅当 :empty 时,才选择#leftmenu that .parent() 抓取到

This only selects #leftmenu if it's :empty, and then only grabs the .parent() of that to .remove(). If it wasn't empty, then the first selector won't find anything, or any parent to remove either.

这篇关于删除或隐藏div(如果为空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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