<分区>
标签 javascript jquery html css
<分区>
示例: http://partnerswealth.advisorproducts.com/home
http://mah.advisorproducts.com/home
要求:
我想在它为空时隐藏 Left 和 Right div 或 Box。
下面是我使用的 HTML 代码:
<div class="Box secondaryBackground">
<div class="innerBox containerBackground">
<h2> </h2>
<div class="innerBoxContent">
<div class="eNews"> </div>
</div>
</div>
</div>
下面是我正在使用的 js - 并尝试仅在 innerBoxContent 子 div 为空的 Box 类 div 中添加 DispNone 类.
所以我只想在 Box 类 div 中添加类,如果像 eNews 这样的 innerBoxContent 类 div 子级是空的。
$(document).ready(function () {
str = $('.innerBoxContent > div').text();
if ($.trim(str) === "") {
$('.innerBox').parent().addClass('DispNone');
}
else {
$('.innerBox').parent().removeClass('DispNone');
}
});
我们可以修改这段代码吗?
现在它向所有 innerBox div 添加类,但我只想要那些最后一个子元素为空的 div,例如 .eNews,而不是所有。 谢谢 寿司
最佳答案
你应该能够像这样遍历你的元素:
$('.innerBoxContent').each(function() {
var noContent = 0;
var count = $(this).children().length;
$(this).children().each(function() {
if ($(this).html == '') {
++noContent;
}
});
if (noContent === count) {
$(this).hide();
}
});
上面考虑到每个 innerBoxContent 可以有多个 child ,有些可能是空的,有些可能不是。
它未经测试,如果有更聪明的方法,我不会感到惊讶:)
关于javascript - 空白的div如何隐藏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13880270/