2011-12-23 135 views
2

下面的代码可以检查图像是否可见。如何检查图像是否可见?

$('#div1 img:visible') 

将选择所有图像后裔和:

$('#div1 > img:visible') 

我只需要知道,当我像dgImages $("#dgImages] img").each(function() {}容器每个图像遍历我怎么能确定该图像是可见或不可见?我可以写些类似if($(this:visible)){//Do something}的东西吗?谢谢。

+0

首先查看jQuery文档http://docs.jquery.com/Main_Page – Hoque 2011-12-23 08:42:57

回答

5
$("#dgImages").find('img').each(function(){ 
    if($(this).is(':visible')){ 
    alert("This image is visible"); 
    } 
}); 
4

您可以使用is()来检查你的对象反对任何选择:

if($(this).is(':visible')) { ... } 
4

您可以使用.is()

if ($(this).is(':visible')) { 
    ... 
1

此页面演示了另一种方法使用显示: http://acarna.com/vis-test.php

而不是看每一个图像我已经用一个类来识别需要切换的特定图像组。单击“切换”按钮测试.css(“显示”)以获取当前显示状态,并将其切换为内联或不相应。

在上面的页面查看源代码以获取更多详细信息。

如果您安装了Firebug,您可以观看标记会发生什么情况,因为显示属性在内联和非内嵌之间切换。