javascript 显示错误信息 3 秒然后淡出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27498976/
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-10-28 07:33:43  来源:igfitidea点击:

Display error message for 3 seconds and then fades out

javascriptphpjqueryhtmlcss

提问by Illep

Based on what my PHP file returns i want to show a message on top of the Web page.

根据我的 PHP 文件返回的内容,我想在网页顶部显示一条消息。

$msg = "Success"; // or fail
$redirecturl = "index.php?msg=".$msg;
header("Location: $redirecturl");

HTML code

HTML代码

<?php   $msg=isset($_GET['msg']) ? $_GET['msg'] : "";?>
<div id="display-success"><?php echo $msg; ?></div>

Now if i get the Successmessage i need to print a small box that appears (for 3 seconds, then fades out) that shows the message successin a greenbackground. And if its failurethen i want to display a message in a Redbackground for 3 seconds and then fades out.

现在,如果我收到Success消息,我需要打印一个出现的小框(持续 3 秒,然后淡出),success绿色背景显示该消息。如果它 failure然后我想在红色背景中显示一条消息3 秒钟,然后淡出。

回答by 4dgaurav

//by using setTime0ut

setTimeout(function() {
   $('#display-success').fadeOut().text('')
}, 10000 );

//by using delay()

$('#display-success').fadeIn().delay(10000).fadeOut();



例子 Demo演示

jquery

查询

$(".showMsg").click(function () {
    $('#display-success').fadeIn().delay(3000).fadeOut();
});

html

html

<button class="showMsg">Click Me</button>
<div id="display-success">The error Message</div>