如何在 Bootstrap 中创建警告通知?

bootstrapweb developmentfront end technology

Bootstrap 提供了一组类和组件来创建警报消息,这些消息可用于通知用户不同类型的信息,例如错误、警告和成功消息。警告警报通常用于告知用户网站或应用程序中的问题或错误。

Bootstrap 中的"alert-warning"类用于发出警报并向用户指示警告消息。您可以使用不同的类(例如 alert-success、alertdanger、alert-info 等)向用户传达不同类型的消息,例如成功、危险、信息等。

方法

在 Bootstrap 中创建警告通知警报有两种不同的方法,包括:

  • 使用"带链接的警报"

  • 使用"可关闭警报"

让我们详细了解每个步骤。

方法 1:使用"带链接的警报方法"

第一种方法是在 Bootstrap 中将警告通知警报创建为"带链接的警报"。带链接的警报是一种警告消息,其中包含一个链接,用于提供更多信息或将用户带到特定页面。这种类型的警报通常用于通知用户网站或应用程序中的潜在问题或错误。要在 Bootstrap 中创建带有链接的警报,您可以在警报元素内的锚标记 (<a>) 上使用"alert-link"类。

示例

以下是在 Bootstrap 中使用"带链接的警报"方法创建警告通知警报的示例。

<!DOCTYPE html>
<html>
<head>
   <title>Bootstrap Alert with Link</title>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" />
</head>
<body>
   <div class="container py-5">
      <h4 class="text-center text-uppercase">
         Bootstrap Alert with Link
      </h4>
      <div class="alert alert-warning">
         <strong>Warning!</strong>
         There was a problem with the connection. <a href="#" class="alert-link">Please contact support.</a>
      </div>
   </div>
</body>
</html> 

方法二:使用"Dismissible 方法"

第二种方法是在 Bootstrap 中创建"Dismissible Alert"类型的警告通知。它允许用户通过点击关闭按钮轻松关闭警报。方法是将"alert-dismissible"类添加到警报中,该类会显示一个关闭按钮,用户可以点击该按钮关闭警报。

示例

以下是使用"Dismissible Alert 方法"在 Bootstrap 中创建警告通知的示例。

<!DOCTYPE html> 
<html>
<head>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" />
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</head>
<body>
   <div class="container ">
      <h4 class="text-center text-uppercase">
         Example of a warning alert with a button
      </h4>
      <div class="alert alert-warning alert-dismissible fade show" role="alert">
         <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">×</span>
         </button>
         <h5 class="alert-heading">Warning</h5>
         <p>"This is a warning message. Please be aware that the content of this page may be outdated and should be used for informational purposes only. Any decisions or actions taken based on this information are the sole responsibility of the user. We recommend seeking professional advice before making any important decisions. Thank you for your understanding."</p>
         <hr>
         <p>Click the button to dismiss this alert.</p>
         <button type="button" class="btn btn-secondary mt-3" datadismiss="alert">Dismiss</button>
      </div>
   </div>
</body>
</html>

结论

在本文中,我们研究了在 Bootstrap 中创建警告通知警报的两种不同方法。这里,我们使用了两种不同的方法:"带链接的警报"和"可关闭的警报"。"带链接的警报"方法涉及创建一条警告消息,其中包含一个链接,用于提供更多信息或将用户带到特定页面。"可关闭的警报"方法涉及创建一条可由用户关闭的警告消息。这两种方法各有优缺点,可根据项目的具体需求进行使用。


相关文章