jQuery 在一定时间后删除引导程序警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16604407/
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
jQuery remove bootstrap alert after certain amount of time
提问by django
I am using dynamic bootstrap alerts from an example. See below.
我正在使用示例中的动态引导程序警报。见下文。
How can I add a timeout function so alert closes automatically after Xtime ?
如何添加超时功能以便警报在X时间后自动关闭?
HTML:
HTML:
<div id="alert_placeholder"></div>
JQUERY:
查询:
bootstrap_alert = function() {
}
bootstrap_alert.warning = function(message) {
$('#alert_placeholder').append('<div class="alert alert-block fade in"><button type="button" class="close" data-dismiss="alert">×</button><h4>Info!</h4>'+ message +'</div>');
}
bootstrap_alert.info = function(message) {
$('#alert_placeholder').append('<div class="alert alert-block alert-info fade in"><button type="button" class="close" data-dismiss="alert">×</button><h4>Info!</h4>'+ message +'</div>');
}
回答by George
Create a function that removes the first (hence, oldest) alert:
创建一个删除第一个(因此是最旧的)警报的函数:
function alertTimeout(wait){
setTimeout(function(){
$('#alert_placeholder').children('.alert:first-child').remove();
}, wait);
}
[0]
to ensure that only the first alert gets removed, for each timeout.
[0]
确保每次超时仅删除第一个警报。
And then call the function when you show the alert, with the parameter being how long until the alert closes, in milliseconds:
然后在显示警报时调用该函数,参数是警报关闭的时间,以毫秒为单位:
bootstrap_alert.warning = function(message) {
$('#alert_placeholder').append('<div class="alert alert-block fade in"><button type="button" class="close" data-dismiss="alert">×</button><h4>Info!</h4>'+ message +'</div>');
alertTimeout(5000);
}
Please see this jsFiddle
请看这个jsFiddle
回答by danny
try this one
试试这个
$(function () {
setTimeout(function () {
if ($(".alert").is(":visible")){
//you may add animate.css class for fancy fadeout
$(".alert").fadeOut("fast");
}
}, 3000)
});