jQuery 检查所有子元素是否被隐藏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10382353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if all children elements are hidden
提问by ggzone
I'm a bit stuck here with my script:
我的脚本有点卡在这里:
Its a checkbox filtering all .notme
images and hiding it's list items.
The problem is now I cant get a working callback function for the fadeToggle
.
It should behave like this :
它是一个过滤所有.notme
图像并隐藏其列表项的复选框。现在的问题是我无法为fadeToggle
. 它的行为应该是这样的:
If all children of
#list-team-single-container
are "displayed none" - do something.
如果所有的孩子
#list-team-single-container
都“没有显示” - 做点什么。
$('#show-only-my-teams').change(function(){
$('.notme').each(function(){
$(this).parent().parent().fadeToggle('fast', function(){
});
});
});
回答by thecodeparadox
if($('#list-team-single-container').children(':visible').length == 0) {
// action when all are hidden
}
回答by Lix
The :visible
jQuery selector might be what you are looking for...
在:visible
jQuery选择可能是你在找什么?
From the description
从描述来看
Elements are considered visible if they consume space in the document. Visible elements have a width or height that is greater than zero.
Elements with visibility: hidden or opacity: 0 are considered visible, since they still consume space in the layout. During animations that hide an element, the element is considered to be visible until the end of the animation. During animations to show an element, the element is considered to be visible at the start at the animation.
如果元素占用文档中的空间,则元素被视为可见。可见元素的宽度或高度大于零。
具有可见性:隐藏或不透明度:0 的元素被认为是可见的,因为它们仍然占用布局中的空间。在隐藏元素的动画期间,元素被认为是可见的,直到动画结束。在显示元素的动画期间,该元素在动画开始时被认为是可见的。
http://api.jquery.com/visible-selector/
http://api.jquery.com/visible-selector/
$('#list-team-single-container').children(':visible');
That line of code will return all of the child elements of #list-team-single-container
that are visible.
这行代码将返回所有#list-team-single-container
可见的子元素。
$('#list-team-single-container').children(':visible').length;
That line of code will return the number of child elementsof #list-team-single-container
that are visible.
该行代码将返回子元素的数量的#list-team-single-container
是可见的。
回答by rgin
It's difficult to be specific without seeing your markup, but I'd imagine doing something like this:
如果没有看到您的标记,很难具体说明,但我可以想象做这样的事情:
var isVisible = 0;
$('.notme').each( function() {
if( $(this).is(":visible") {
isVisible++;
}
});
if ( isVisible == 0 )
// do something