jQuery 如何在几秒钟后隐藏 Flash 消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31176402/
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
How to hide flash message after few seconds?
提问by sweety
In my application user can post challenge for other user. So after successful posting a challenge I am displaying one flash message for the same. But now I want to hide this message after some few seconds. So I wrote following code :
在我的应用程序中,用户可以为其他用户发布挑战。因此,在成功发布挑战后,我会为此显示一条 Flash 消息。但现在我想在几秒钟后隐藏这条消息。所以我写了以下代码:
$(document).ready(function(){
setTimeout(function() {
$("#successMessage").hide('blind', {}, 500)
}, 5000);
});
<div id="successMessage" style="text-align:center; width:100%">
<FONT color="green">
<%if flash[:alert]=="Your challenge is posted successfully."%>
<h4><%= flash[:alert] if flash[:alert].present? %>
<%end%>
</font>
</div>
But this code is not hiding the div "successMessage".
但是这段代码并没有隐藏 div“successMessage”。
回答by Puja
You can try :
你可以试试 :
setTimeout(function() {
$('#successMessage').fadeOut('fast');
}, 30000); // <-- time in milliseconds
If you used this then your div will be hide after 30 sec.I also tried this one and it worked for me.
如果你使用它,那么你的 div 将在 30 秒后隐藏。我也试过这个,它对我有用。
回答by m Piroli
You can use the delay jQuery APIto achieve this.
您可以使用延迟 jQuery API来实现这一点。
$(document).ready(function(){
$("#successMessage").delay(5000).slideUp(300);
});
回答by Justin Feistner
Someone posted a similar question to stackoverflow with this solution:
有人用这个解决方案向 stackoverflow 发布了一个类似的问题:
<script type="text/javascript">window.setTimeout("document.getElementById('successMessage').style.display='none';", 2000); </script>
<div id="successMessage"> bla bla bla
</div>
I'd share the link but I can't find it anymore. Thanks for the help, though, mystery human!
我想分享链接,但我找不到了。谢谢你的帮助,不过,神秘的人类!
回答by Qasim Nadeem
Some times it's not enough to only setting display of box to none it's better to completely remove it. as follows:
有时仅将框的显示设置为无是不够的,最好将其完全删除。如下:
setTimeout(function() {
$('.alert-box').remove();
}, 30000);