Javascript jQuery $('#div').show().delay(5000).hide(); 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7288669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:38:30  来源:igfitidea点击:

Jquery $('#div').show().delay(5000).hide(); doesn't work

javascriptjquerycss

提问by DexCurl

I'm trying to show a div thats set to display: none;for 5 seconds with

我正在尝试显示一个设置display: none;为 5 秒的 div

$('#div').show().delay(5000).hide();

but it deson't work, it just goes straight to hide()

但它不起作用,它只是直接进入 hide()

Can any of you help me?

你们中的任何人都可以帮助我吗?

回答by jAndy

Do it like this:

像这样做:

$('#div').show(0).delay(5000).hide(0);

By passing in numbers to .show()and .hide(), jQuery will take those methods into its internal fx queue(even if the number is zero). Since .delay()only works within a queue, you need that little workaround.

通过将数字传递给.show().hide(),jQuery 会将这些方法放入其内部fx 队列(即使数字为零)。由于.delay()仅在队列中工作,因此您需要一点解决方法。

example: http://jsfiddle.net/zceKN/

示例:http: //jsfiddle.net/zceKN/

回答by gilly3

You need to use .queue()because .hide()isn't queued by default.

您需要使用,.queue()因为.hide()默认情况下不排队。

$("#div").show().delay(5000).queue(function (next) {
    $(this).hide();
    next();
});

回答by Paul

You need a duration on your hide for it to work:

您需要在隐藏上持续一段时间才能使其工作:

$('#div').show('slow').delay(5000).hide('slow');

Example: http://jsfiddle.net/Paulpro/GLTaB/

示例:http: //jsfiddle.net/Paulpro/GLTaB/

回答by genesis

$('#div').show();
setTimeout(function(){$('#div').hide();}, 5000);

.delay() works for animations only

.delay() 仅适用于动画