如果下一个为空,则隐藏元素 [英] Hide an element if the next one is empty

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

问题描述

我有以下代码:

<h3 class="hideIfDivEmpty">title</h3>
<div id="divId"></div>

当div为空时,我想隐藏h3元素。
我愿意改变html结构,但h3必须在div之外,因为它的内容是动态改变的。

I would like to hide the h3 element when the div is empty. I'm willing to change the html structure but the h3 has to be outside of the div because its content is dynamically changed.

有办法在CSS中做这个?

Is there a way to do that in CSS ?

推荐答案

没有语法选择父元素或任何其他非子元素 #divid 。您可以选择 #divid (如果它为空) #divid:empty ,但没有办法选择 .hideIfDivIsEmpty 在任何浏览器中选择。根据这个问题,在CSS4中有这样的事情( specs ),但它现在不支持任何浏览器,并根据相同的规格,选择器太慢,无法在浏览器中实施。

There is no syntax to select a parent element or any other non-child element from #divid. You can select #divid if it's empty, by #divid:empty, but there is no way you can select .hideIfDivIsEmpty in any browser by selecting that. According to this question there is such a thing in CSS4 (specs), but it is not supported in any browser as of now and according to the same specs, the selector is too slow to be implemented in browsers.

查看此问题的javascript解决方案的其他答案。

See the other answers for the javascript solution to this problem.

这篇关于如果下一个为空,则隐藏元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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