jQuery隐藏对象检查 [英] JQuery hidden objects check

查看:88
本文介绍了jQuery隐藏对象检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
JQuery隐藏的对象

Possible Duplicate:
JQuery hidden objects

我还是第一次发布此问题,许多人不理解.好的,我将再次更改我的问题,删除混乱的部分.

I am posting the issue again as the first time, many did not understand. Ok, I will change my question again, removing the confusion part.

我有4张具有不同ID的图像.我不能在这里使用类,因为每个图像都与另一件事相关,因此请在这里忘记类以避免混淆.将会发生什么,当用户单击每个图像时,它将隐藏.因此,点击4张图片后,这4张图片当然会被隐藏.

I have 4 images with different IDs. I cannot use class here as each image is related to another thing, so forget class here to avoid confusion. What is going to happen, when the user clicks on each image it will hide. So, after clicking on 4 images, the 4 will be hidden of course.

我希望隐藏4张图像后显示一个警告框.请,我必须提供一个代码来验证所有图像是否都隐藏在这里,不要问我为什么避免再次造成混淆.单击一次即可隐藏图像,但是唯一的问题是隐藏了4张图像后没有显示警告框.下面的代码:

I want that after the 4 images are hidden, an alert box is displayed. Please, I have to include a code to verify if all images are hidden here, do not ask me why to avoid confusing again. The images are hiding once clicked, but the only issue is the alert box is not displayed after the 4 images are hidden. Code below:

$(document).ready(function() {

    $('#image1').click(function() {
        $(this).hide('slow');
    });

    $('#image2').click(function() {
        $(this).hide('slow');
    });

    $('#image3').click(function() {
        $(this).hide('slow');
    });

    $('#image4').click(function() {
        $(this).hide('slow');
    });

    if ($('#image1, #image2, #image3, #image4').is(':hidden')) {
        alert('kkk');
    }
});​

推荐答案

首先,您可以通过对所有以image开头的元素使用选择器来消除重复.

First of all, you can eliminate repetition by using the selector for all elements that start with image.

$("[id^=image]")选择所有ID以image开头的元素.

$("[id^=image]") selects all elements whose id starts with image.

然后,您必须提供hide回调函数,以便它仅在动画结束时运行:

Then you have to provide the hide callback function, so that it only runs when animation ends:

$(document).ready(function() {
    $("[id^=image]").click(function() {
        $(this).hide('slow', function() {
            if ($('[id^=image]:visible').length == 0) {
                alert('I am a bunny');
            }
        });
    });
});​

这篇关于jQuery隐藏对象检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆