admin管理员组

文章数量:1317906

I have a collection of elements on my page, and I want to see if they are visible or not currently.

So:

$(".someClass")

How can I loop through and figure this out? because if it is visible, i have to fire another function.

I have a collection of elements on my page, and I want to see if they are visible or not currently.

So:

$(".someClass")

How can I loop through and figure this out? because if it is visible, i have to fire another function.

Share Improve this question asked Apr 16, 2009 at 19:48 BlankmanBlankman 267k332 gold badges795 silver badges1.2k bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 9
$(".someClass").each(function(){
  if($(this).is(":visible")){
    //item is visible: do something
   }
});

how about that?

$(".someClass:visible")

will return the visible ones.

What you could do:

$(".someClass").each(function(x) { if ( x.style.display != "none" && x.style.visibility != "hidden" ) { your_function(); } });

where your_function() is the name of your function.

All solutions in terms of $('.someClass').is(':visible') are unreliable. All it tells us is if a particular element has a styling of display:none or visibility:hidden. This is not the same as whether an element is visible!

Consider a situation like this:

<div style="display:none;"><div class="someClass"></div></div>

Everybody can see that the element designated by $('.someClass') is invisible. But $('.someClass').is(':visible') will return true!

The only water-proof solution is to not only check for is(':visible') on the $('.someClass'), but on all of its parent elements as well. Only if for none of the parents holds that is(':visible') === false, we can conclude that $('.someClass') is actually visible.

本文标签: