Bootstrap自动关闭动态添加警报

时间:2022-08-22 11:59:32

So I add this code dynamically when the user clicks a button (data is passed in as a parameter from an AJAX call, that all works fine):

因此,当用户单击按钮时,我会动态添加此代码(数据作为AJAX调用的参数传入,一切正常):

$('#container').append('
<div class=" alert alert-danger alert-dismissable fade in" role="alert">    
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
    <h4>Error</h4>
    <p>'+data+'</p>
</div>');`

I was wondering if it was possible to add functionality to auto close this alert after a certain time. I know there are other ways to go about doing this where I could easy accomplish this, I'm just looking to do it this way because I want to have multiple alerts when the same button is clicked, so having each alert handle itself and disappear automatically would make it look a lot cleaner.

我想知道是否有可能在一段时间后添加自动关闭此警报的功能。我知道还有其他方法可以做到这一点,我可以很容易地实现这一点,我只是想这样做,因为我想在点击相同的按钮时有多个警报,所以让每个警报处理自己并消失自动使它看起来更清洁。

1 个解决方案

#1


0  

Sounds like you can use setTimeout Javascript function: https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout

听起来你可以使用setTimeout Javascript函数:https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout

#1


0  

Sounds like you can use setTimeout Javascript function: https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout

听起来你可以使用setTimeout Javascript函数:https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout