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>

这不起作用,但我真的不知道为什么。需要做什么:检查所有图像是否可见。如果它们都不可见,则显示一个段落。

谢谢。

4

3 回答 3

1

我们可以使用 selector 来找出可见的 img 控件。$("img:visible")为您提供可见的 jQuery img 对象数组。通过检查长度,我们发现有多少 img 控件是可见的,这length = 0意味着选择器没有返回任何元素means none of img is visible

if($("img:visible").length == 0)
{
  //show graph
}
于 2012-07-09T17:23:33.403 回答
1

您的代码所做的是在任何图像被隐藏时显示该段落,如果所有图像都被隐藏,您想要显示它:

var shown = false;
$("img").each(function() {
  if($(this).is(':hidden')) {
    shown = true;
  }
});
if (!shown) {
  $("p#nothing").show();
}
于 2012-07-09T17:25:19.277 回答
0

试试这个:

if ($("img:hidden").length > 0) {
    $("p#nothing").show();
}
于 2012-07-09T17:25:16.413 回答