Bootstrap 如何在几秒后自动关闭警报?

How to auto close an alert after few seconds with Bootstrap?

我开发了一个 Python/Flask 应用程序并 运行 解决了一个实施 Bootstrap 警报的问题,其想法是:

代码

<div class="container">
        {% for message in get_flashed_messages() %}   
        <script>       
          function close_alert()
          {
            document.getElementById("my_alert").close()
          }
          setTimeout("close_alert()", 2000)
        </script>
        <div id="my_alert" class="alert alert-primary alert-dismissible fade show" role="alert">
          <strong>{{message}}</strong>
          <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endfor %}
</div>

我是 Bootstrap 的新手,非常感谢您的支持!

谢谢!

您可以使用 Bootstrap 方法来实现此目的。 Take a look here 在页面底部获取更多信息。

逻辑

单击该按钮时,jQuery 删除警报元素的 class d-none,使其可见。然后使用 setTimeout() JS 将等待 2000 毫秒,然后使用 alert() bootstrap 4 方法关闭警报。

只需单击一下即可。

注意:使用Bootstrap 4 种方法 需要使用jQuery.

$('#btn').click(function(){

  $('#alert').removeClass('d-none');
  
  setTimeout(() => {
    $('.alert').alert('close');
  }, 2000);
  
})
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" crossorigin="anonymous">
 
<div id="alert" class="alert alert-primary m-3 d-none" role="alert">
  This is a primary alert—check it out!
</div>

<button class="btn btn-primary m-3" id="btn">
  show alert and close it in 2 seconds
</button>

与普通JavaScript

每次用户单击该按钮时,此代码都会显示警报。如果您不希望这样,您可以使用布尔值或会话存储变量并结合条件语句使其仅工作一次。

const btn = document.getElementById('btn'),
      alert = document.getElementById('alert');
      
btn.addEventListener('click', () => {
  
  alert.classList.remove('d-none');
  
  setTimeout(() => {
    alert.classList.add('d-none');
  }, 2000)
  
})
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" crossorigin="anonymous">
 
<div id="alert" class="alert alert-primary m-3 d-none" role="alert">
  This is a primary alert—check it out!
</div>

<button id="btn" class="btn btn-primary m-3">
  show alert and close it in 2 seconds
</button>

我建议使用“toast”——这是一个 UI 组件,适用于您的用例:https://getbootstrap.com/docs/4.2/components/toasts/

来自 Bootstrap 文档:

<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
  <div class="toast-header">
    <img src="..." class="rounded mr-2" alt="...">
    <strong class="mr-auto">Bootstrap</strong>
    <small class="text-muted">11 mins ago</small>
    <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="toast-body">
    Hello, world! This is a toast message.
  </div>
</div>