を働いていない私は、異なる期間で私のWebページ上の各アラートを閉じautoにシンプルなjQueryのスクリプトを書きました。ブートストラップのアラートの自動閉鎖が
これは私のjQueryのコードです:ここで
$(function() {
var alert = $('div.alert[auto-close]');
alert.each(function() {
var time_period = $(this).attr('auto-close');
setTimeout(function() {
$(this).alert('close');
}, time_period);
});
});
は私の2つのサンプルアラートです:
<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>
各アラートは、アラート閉鎖期間として使用されている値を持つカスタム属性を持っています。
何らかの理由でアラートの終了が機能していません。
試し: 'VAR this_alert = $(この);' TIME_PERIODするvar以下。 'setTimeOut'コールバックスコープ内で' $(this)).alert( 'close'); 'do:' this_alert.alert( 'close') ' – dMd
' $(this) 'とは異なります。それをキャッシュする。下の私の答えをチェックしてください。 – Iceman
ええ、今私はエラーの原因を理解しています。 – Great9