Javascript 引导警报自动关闭不工作
我编写了一个简单的JQuery脚本,可以在不同的时间段自动关闭网页上的每个警报 这是我的JQuery代码:Javascript 引导警报自动关闭不工作,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,我编写了一个简单的JQuery脚本,可以在不同的时间段自动关闭网页上的每个警报 这是我的JQuery代码: $(function() { var alert = $('div.alert[auto-close]'); alert.each(function() { var time_period = $(this).attr('auto-close'); setTimeout(function() {
$(function() {
var alert = $('div.alert[auto-close]');
alert.each(function() {
var time_period = $(this).attr('auto-close');
setTimeout(function() {
$(this).alert('close');
}, time_period);
});
});
以下是我的两个示例警报:
<div class="alert alert-danger alert-dismissible" role="alert" auto-close="3000">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
alert one
</div>
<div class="alert alert-success alert-dismissible" role="alert" auto-close="5000">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
alert two
</div>
&时代;
警报一
&时代;
警报二
每个警报都有一个值为的自定义属性,该值用作警报关闭时间段
由于某些原因,警报关闭不起作用。$(此)
在设置超时
范围内更改。将其缓存在变量中<在我的例子中,code>就是这样
$(function() {
var alert = $('div.alert[auto-close]');
alert.each(function() {
var that = $(this);
var time_period = that.attr('auto-close');
setTimeout(function() {
that.alert('close');
}, time_period);
});
});
工作片段:
$(函数(){
var alert=$('div.alert[自动关闭]);
alert.each(函数(){
var,该值=$(此值);
var time_period=that.attr('auto-close');
setTimeout(函数(){
那。警报(‘关闭’);
},时间段);
});
});代码>
&时代;
警报一
&时代;
警报二
try:var this\u alert=$(this)代码>低于时间周期变量。然后在setTimeout中,而不是$(此).alert('close')代码>执行:此警报。警报('close')
$(此)
在设置超时
回调范围内不同。缓存它。检查我下面的答案。是的,现在我明白了错误的原因。哇,谢谢冰人,它起作用了。你是个救生员:)我会在7分钟后接受答案。