5秒后jQuery自动隐藏元素 [英] jQuery autohide element after 5 seconds

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

问题描述

使用jQuery加载表单后5秒钟是否可以自动隐藏网页中的元素?

Is it possible to automatically hide an element in a web page 5 seconds after the form loads using jQuery?

基本上,我有

<div id="successMessage">Project saved successfully!</div>

我想在5秒后消失.我已经看过jQuery UI和hide效果,但是在按我想要的方式工作时遇到了一些麻烦.

that I'd like to disappear after 5 seconds. I've looked at jQuery UI and the hide effect but I'm having a little trouble getting it work the way I want it to.

<script type="text/javascript">
        $(function() {
            function runEffect() {

                var selectedEffect = 'blind';

                var options = {};

                $("#successMessage").hide(selectedEffect, options, 500);
            };

            $("#successMessage").click(function() {
                runEffect();
                return false;
            });
        });
    </script>

我希望删除click函数,并添加一个5秒后调用runEffect()的超时方法.

I'd like the click function to be removed and add a timeout method that calls the runEffect() after 5 seconds.

mucho gracias!

mucho gracias!

推荐答案

$(function() {
    // setTimeout() function will be fired after page is loaded
    // it will wait for 5 sec. and then will fire
    // $("#successMessage").hide() function
    setTimeout(function() {
        $("#successMessage").hide('blind', {}, 500)
    }, 5000);
});

注意:为了使jQuery函数在setTimeout中工作,您应该将其包装在其中

Note: In order to make you jQuery function work inside setTimeout you should wrap it inside

function() { ... }

这篇关于5秒后jQuery自动隐藏元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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