如果隐藏了子对象,如何隐藏父对象
How to hide parent if children are hidden?
问题:如果包含在父div中的列表中的所有子div都被隐藏,那么我需要在jQuery中找到一种方法来隐藏父div。
上下文:我正在开发一个允许管理员上传PDF的员工门户网站。当他们上传PDF时,他们可以将其附加到一个类别,也可以向其中添加标记。标记的目的是用于内联过滤功能,只有当他们从菜单中选择特定标记时,才会显示与该标记关联的文件。我的标记的一个简化示例:
<!-- Filtering Menu -->
<ul class="resource-filters">
<li class="all">All</li>
<li class="filter-example">Filter #1</li>
<li class="filter-example">Filter #2</li>
<li class="filter-example">Filter #3</li>
</ul>
<!-- Category #1 -->
<div class="resources-files-container">
<h3>Category #1</h3>
<ul class="attachment-list">
<li class="filter-example-1">
<a href="#">PDF Example #1</a>
</li>
<li class="filter-example-3">
<a href="#">PDF Example #3</a>
</li>
</ul>
</div>
<!-- Category #2 -->
<div class="resources-files-container">
<h3>Category #2</h3>
<ul class="attachment-list">
<li class="filter-example-2 hidden">
<a href="#">PDF Example #2</a>
</li>
<li class="filter-example-2 hidden">
<a href="#">PDF Example #2</a>
</li>
</ul>
</div>
我遇到的问题是,假设我从菜单中选择"过滤器#1"。所有不具有.filter-example-1
类的列表项都将设置为display: none
(隐藏类)。在上面的示例标记中,这意味着类别#2现在没有要显示的项。我希望隐藏.resources-files-container
的整个div(直到他们选择另一个合适的过滤器)。
我已经尝试解决这个问题几个小时了,并进行了大量搜索(我找到了一些符合我的部分标准的解决方案,但不是这个特定的案例)。除了这个特殊的部分,我已经弄清楚了整个应用程序。下面的jQuery与我遇到的这个特定问题并不特别相关,只是为了给你一些上下文:
// only run if we're in the portal
if ($("#portal-container").length) {
// get the unique list of classnames
var classes = {};
$('.attachment-list li').each(function() {
$($(this).attr('class').split(' ')).each(function() {
if (this !== '') {
classes[this] = this;
}
});
});
//build the list items
class_list = '';
for (class_name in classes) {
class_list += '<li class="'+class_name+'">'+class_name+'</li>';
};
// append the resulting list to the page
$(class_list).appendTo('#resource-filter ul');
// sorting functionality
$('#portal-page').on('click', '#resource-filter li', function() {
// set the current filter
var filterVal = $(this).attr('class');
// add current state to filter button
$('#resource-filter li.current').removeClass('current');
$(this).addClass('current');
// filtering function
if(filterVal == 'filter-all') {
$('.attachment-list li.hidden').fadeIn('slow').removeClass('hidden');
} else {
$('.attachment-list li').each(function() {
if(!$(this).hasClass(filterVal)) {
$(this).fadeOut('normal').addClass('hidden');
} else {
$(this).fadeIn('slow').removeClass('hidden');
}
});
}
});
}
我非常感谢你的建议。
$(".resources-files-container")
.filter(function(){
return $(this).find("li:visible").length == 0;
})
.hide();
将.resources-files-container
元素的列表筛选为仅具有不可见li
元素的列表,然后隐藏该筛选集。
http://jsfiddle.net/n403agxn/
如果选择发生更改,还包括一个else来切换回可见性:
function toggleContainersByChildren() {
$('.resources-files-container').each(function () {
if ($(this).find('li:visible').length === 0) { // note the three = signs
$(this).hide(); //hides the container if no children are visible
} else {
$(this).show(); //shows it again if the children are turned back on
}
});
}
toggleContainersByChildren(); //call this function to check your containers for visible children every time the filter is changed
编辑:提请注意===等号…
如果我理解正确,您需要检查div .resources-files-container
中的所有li
是否都没有.filter-example-1
类。。。
所以,它应该是这样的:
//Select all div with this class, and iterate through them
$('.resources-files-container').each(function() {
//Checks if the number of li's with the specified class inside this div is "0"
if($(this).find('li.filter-example-1').length == 0) {
//Hides the div, because there's no visible li in it.
$(this).hide();
}
});
- 是否可以在不重新渲染的情况下显示/隐藏父对象中的元素
- 取消隐藏网站中的隐藏对象
- 编程实践-使用helper方法隐藏对象
- 如果鼠标离开父对象,则隐藏元素
- 从范围中删除对象时,它会在 Angularjs 中隐藏其余对象
- 将字符串隐藏到javascript中的对象
- 使用javascript函数检索隐藏的文本值时出现对象HTMLinputElement错误
- 在 JavaScript 中,当我们实例化派生对象时,原型的函数隐藏在哪里
- angularjs 选中复选框时隐藏具有特定类的对象
- 在Angular.JS-ng隐藏vs过滤器中将对象分隔为两组
- handontable:在不更改数据数组/对象的情况下隐藏某些列
- Javascript对象字符串值-在应该隐藏的时候呈现的HTML代码
- Three.js-显示/隐藏对象的一部分
- Three.js隐藏已加载模型的对象
- 将日期字符串隐藏到日期对象中
- 将对象隐藏到组合/对象数组
- 使可见对象隐藏和隐藏对象可见
- 将JS对象隐藏到具有父名称的平面数组中
- 将JSON对象中的JSON对象隐藏到Javascript中的JSON数组中
- 一个构造函数,其对象隐藏/显示一个html元素