2012-07-09 62 views
1

檢查每個圖像是否可見。如果沒有看到,做一些

$("img").each(function() { 
 
    if($(this).is(':hidden')) { 
 
     $("p#nothing").show(); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="imgWrap"> 
 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/9396/326/800/9396326800_4_1_4.jpg" class="fashion black" alt=""> 
 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/9396/323/401/9396323401_4_1_4.jpg" class="fashion" alt=""> 
 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/9396/306/622/9396306622_4_1_4.jpg" class="fashion pink" alt=""> 
 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/5396/361/707/5396361707_4_1_4.jpg" class="fashion brown" alt=""> 
 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/5396/302/800/5396302800_4_1_4.jpg" class="fashion pink" alt=""> 
 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/5396/377/800/5396377800_4_1_4.jpg" class="fashion black" alt=""> 
 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/5396/349/725/5396349725_4_1_4.jpg" class="fashion beige" alt=""> 
 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/5639/306/401/5639306401_4_1_4.jpg" class="jumpsuit blue" alt=""> 
 
</div>

$("img").each(function() { 
    if($(this).is(':hidden')) { 
     $("p#nothing").show(); 
    } 
}); 

HTML:

<div id="imgWrap"> 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/9396/326/800/9396326800_4_1_4.jpg" class="fashion black" alt=""> 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/9396/323/401/9396323401_4_1_4.jpg" class="fashion" alt=""> 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/9396/306/622/9396306622_4_1_4.jpg" class="fashion pink" alt=""> 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/5396/361/707/5396361707_4_1_4.jpg" class="fashion brown" alt=""> 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/5396/302/800/5396302800_4_1_4.jpg" class="fashion pink" alt=""> 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/5396/377/800/5396377800_4_1_4.jpg" class="fashion black" alt=""> 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/5396/349/725/5396349725_4_1_4.jpg" class="fashion beige" alt=""> 
<img src="http://static.pullandbear.net/2/photos/2012/V/0/1/p/5639/306/401/5639306401_4_1_4.jpg" class="jumpsuit blue" alt=""> 
</div> 

<p id="nothing">No products were found that matched your filters</p> 

這並不工作,但我真的無法找出原因。需要做什麼:檢查所有圖像是否可見。如果它們都不可見,則顯示一段。

謝謝。

+2

它應該工作,你可以發佈的HTML? – undefined 2012-07-09 17:23:13

+0

@undefined:添加了HTML。 – 2012-07-09 17:50:09

+0

您的HTML沒有任何隱藏圖像。 – j08691 2012-07-09 19:29:41

回答

1

我們可以使用選擇器來找出可見的img控件。 $("img:visible")爲您提供了那些可見的jQuery img對象數組。通過檢查長度,我們可以發現有多少img控件可見,並且length = 0表示選擇器means none of img is visible沒有返回任何元素。

if($("img:visible").length == 0) 
{ 
    //show graph 
} 
+0

謝謝@Bram Vanroy,我已經更新了答案,如果您有任何疑問,請不要猶豫。 – Adil 2012-07-10 14:01:20

0

試試這個:

if ($("img:hidden").length > 0) { 
    $("p#nothing").show(); 
} 
1

您的代碼不會有什麼顯示段落,如果任何圖像是隱藏的,你想要的是顯示,如果所有被隱藏:

var shown = false; 
$("img").each(function() { 
    if($(this).is(':hidden')) { 
    shown = true; 
    } 
}); 
if (!shown) { 
    $("p#nothing").show(); 
} 
相關問題