jquery 查找具有类和样式显示的元素:块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19090392/
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
jquery find element with class and style display:block
提问by spiderman
I am retrieving the number of "found" elements (these elements have the .highlight
class) with this simple jQuery snippet:
我正在.highlight
使用这个简单的 jQuery 代码段检索“找到”元素的数量(这些元素具有类):
$(".highlight").length
But now my problem is that some elements are hidden, via style="display: none;"
但现在我的问题是某些元素被隐藏了,通过 style="display: none;"
Now, How can I get the number of elements highlighted AND displayed?
现在,如何获得突出显示和显示的元素数量?
Something like:
就像是:
$(hasClass 'highlight' AND has style 'display: block'). length ?
回答by Adil
回答by dimitrisk
One way is to use :visible
jQuery pseudo selector as Adil mentioned.
一种方法是使用:visible
Adil 提到的 jQuery 伪选择器。
A common pitfall is that if the element with class .highlight
is nested to a container which is hidden then you wont be able to get it even though that element has display: block
一个常见的陷阱是,如果带有 class 的元素.highlight
嵌套到一个隐藏的容器中,那么即使该元素具有display: block
Instead you could use css regex as follows:
$('.highlight[style*="display: block"]')
相反,您可以使用 css regex 如下:
$('.highlight[style*="display: block"]')
A common pitfall is that you need to know exactly how the rule is written. If there is no space before block
like so: display:block
instead of display: block
you wont be able to get the element either.
一个常见的陷阱是您需要确切地知道规则是如何编写的。如果之前没有空格,则block
这样:display:block
而不是display: block
您也将无法获得该元素。
A way to overcome this is to search only for the term block
in the styles like so:
$('.highlight[style*="block"]')
克服这个问题的一种方法是仅搜索block
样式中的术语,如下所示:
$('.highlight[style*="block"]')