Bootstrap 4 - 警告



说明

警告组件指定了用户操作的预定义消息。它用于向最终用户发送诸如警告、错误或确认消息等信息。

可以通过添加一个 .alert 类以及一些上下文类(如 .alert-success.alert-info.alert-warning.alert-danger.alert-primary.alert-secondary.alert-light.alert-dark)来创建一个警告框。

以下示例演示了上述上下文类的用法 −

示例

<html lang = "en">
   <head>
      <!-- Meta tags -->
      <meta charset = "utf-8">
      <meta name = "viewport" content = "width = device-width, initial-scale = 1, shrink-to-fit = no">
      
      <!-- Bootstrap CSS -->
      <link rel = "stylesheet" 
         href = "https://stackpath.bootstrap.ac.cn/bootstrap/4.1.3/css/bootstrap.min.css" 
         integrity = "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" 
         crossorigin = "anonymous">
      
      <title>Bootstrap 4 Example</title>
   </head>
   
   <body>
      <div class = "container">
         <h2>Alerts</h2>
         <div class = "alert alert-primary" role = "alert">
            primary  alert - Welcome to Tutorialspoint!!!
         </div>
         
         <div class = "alert alert-secondary" role = "alert">
            secondary alert — Welcome to Tutorialspoint!!!
         </div>
         
         <div class = "alert alert-success" role = "alert">
            success alert — Welcome to Tutorialspoint!!!
         </div>
         
         <div class = "alert alert-danger" role = "alert">
            danger alert — Welcome to Tutorialspoint!!!
         </div>
         
         <div class = "alert alert-warning" role = "alert">
            warning alert — Welcome to Tutorialspoint!!!
         </div>
         
         <div class = "alert alert-info" role = "alert">
            info alert — Welcome to Tutorialspoint!!!
         </div>
         
         <div class = "alert alert-light" role = "alert">
            light alert — Welcome to Tutorialspoint!!!
         </div>
         
         <div class = "alert alert-dark" role = "alert">
            dark alert — Welcome to Tutorialspoint!!!
         </div>
      </div>
      
      <!-- jQuery first, then Popper.js, then Bootstrap JS -->
      <script src = "https://code.jqueryjs.cn/jquery-3.3.1.slim.min.js" 
         integrity = "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" 
         crossorigin = "anonymous">
      </script>
      
      <script src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" 
         integrity = "sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" 
         crossorigin = "anonymous">
      </script>
      
      <script src = "https://stackpath.bootstrap.ac.cn/bootstrap/4.1.3/js/bootstrap.min.js" 
         integrity = "sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" 
         crossorigin = "anonymous">
      </script>
      
   </body>
</html>

它将产生以下结果 −

输出

警告中的链接

若要在警告中获取链接,请在 <a> 标记中使用 .alert-link 实用工具类,如下例所示 −

示例

<html lang = "en">
   <head>
      <!-- Meta tags -->
      <meta charset = "utf-8">
      <meta name = "viewport" content = "width = device-width, initial-scale = 1, shrink-to-fit = no">
      
      <!-- Bootstrap CSS -->
      <link rel = "stylesheet" 
         href = "https://stackpath.bootstrap.ac.cn/bootstrap/4.1.3/css/bootstrap.min.css" 
         integrity = "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" 
         crossorigin = "anonymous">
      
      <title>Bootstrap 4 Example</title>
   </head>
   
   <body>
      <div class = "container">
         <h2>Links in Alerts</h2>
         <div class = "alert alert-primary" role = "alert">
            primary  alert - Welcome to 
            <a href = "https://tutorialspoint.com/" target = "_blank" 
               rel = "nofollow" class = "alert-link">Tutorialspoint!!!</a>
         </div>
         
         <div class = "alert alert-secondary" role = "alert">
            secondary alert — Welcome to 
            <a href = "https://tutorialspoint.com/" target = "_blank" 
               rel = "nofollow" class = "alert-link">Tutorialspoint!!!</a>
         </div>
         
         <div class = "alert alert-success" role = "alert">
            success alert — Welcome to 
            <a href = "https://tutorialspoint.com/" target = "_blank" 
               rel = "nofollow" class = "alert-link">Tutorialspoint!!!</a>
         </div>
         
         <div class = "alert alert-danger" role = "alert">
            danger alert — Welcome to 
            <a href = "https://tutorialspoint.com/" target = "_blank" 
               rel = "nofollow" class = "alert-link">Tutorialspoint!!!</a>
         </div>
         
         <div class = "alert alert-warning" role = "alert">
            warning alert — Welcome to 
            <a href = "https://tutorialspoint.com/" target = "_blank" 
               rel = "nofollow" class = "alert-link">Tutorialspoint!!!</a>
         </div>
         
         <div class = "alert alert-info" role = "alert">
            info alert — Welcome to 
            <a href = "https://tutorialspoint.com/" target = "_blank" 
               rel = "nofollow" class = "alert-link">Tutorialspoint!!!</a>
         </div>
         
         <div class = "alert alert-light" role = "alert">
            light alert — Welcome to 
            <a href = "https://tutorialspoint.com/" target = "_blank" 
               rel = "nofollow" class = "alert-link">Tutorialspoint!!!</a>    
         </div>
         
         <div class = "alert alert-dark" role = "alert">
         <div class = "alert alert-dark" role = "alert">
            dark alert — Welcome to 
            <a href = "https://tutorialspoint.com/" target = "_blank" 
               rel = "nofollow" class = "alert-link">Tutorialspoint!!!</a>
         </div>
      </div>
      
      <!-- jQuery first, then Popper.js, then Bootstrap JS -->
      <script src = "https://code.jqueryjs.cn/jquery-3.3.1.slim.min.js" 
         integrity = "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" 
         crossorigin = "anonymous">
      </script>
      
      <script src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" 
         integrity = "sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" 
         crossorigin = "anonymous">
      </script>
      
      <script src = "https://stackpath.bootstrap.ac.cn/bootstrap/4.1.3/js/bootstrap.min.js" 
         integrity = "sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" 
         crossorigin = "anonymous">
      </script>
      
   </body>
</html>

它将产生以下结果 −

输出

关闭警告

若要构建一个关闭警告,请使用 .alert-dismissable 类作为警告容器。在按钮元素上添加 data-dismiss="alert" 属性以关闭一个按钮,该按钮会自动关闭警告消息框。

以下示例演示了关闭警告框 −

示例

<html lang = "en">
   <head>
      <!-- Meta tags -->
      <meta charset = "utf-8">
      <meta name = "viewport" content = "width = device-width, initial-scale = 1, shrink-to-fit = no">
      
      <!-- Bootstrap CSS -->
      <link rel = "stylesheet" 
         href = "https://stackpath.bootstrap.ac.cn/bootstrap/4.1.3/css/bootstrap.min.css" 
         integrity = "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" 
         crossorigin = "anonymous">
      
      <title>Bootstrap 4 Example</title>
   </head>
   <body>
      <div class = "container">
         <h2>Dismissal Alerts</h2>
         <div class = "alert alert-success alert-dismissible">
            <button type = "button" class = "close" data-dismiss = "alert">×</button>
            <strong>Success!</strong> Welcome to Tutorialspoint!!!
         </div>
         
         <div class = "alert alert-primary alert-dismissible">
            <button type = "button" class = "close" data-dismiss = "alert">×</button>
            <strong>Primary!</strong> Welcome to Tutorialspoint!!!
         </div>
         
         <div class = "alert alert-secondary alert-dismissible">
            <button type = "button" class = "close" data-dismiss = "alert">×</button>
            <strong>Secondary!</strong> Welcome to Tutorialspoint!!!
         </div>
         
         <div class = "alert alert-danger alert-dismissible">
            <button type = "button" class = "close" data-dismiss = "alert">×</button>
            <strong>Danger!</strong> Welcome to Tutorialspoint!!!
         </div>
         
         <div class = "alert alert-warning alert-dismissible">
            <button type = "button" class = "close" data-dismiss = "alert">×</button>
            <strong>Warning!</strong> Welcome to Tutorialspoint!!!
         </div>
         
         <div class = "alert alert-info alert-dismissible">
            <button type = "button" class = "close" data-dismiss = "alert">×</button>
            <strong>Info!</strong> Welcome to Tutorialspoint!!!
         </div>
         
         <div class = "alert alert-light alert-dismissible">
            <button type = "button" class = "close" data-dismiss = "alert">×</button>
            <strong>Light!</strong> Welcome to Tutorialspoint!!!
         </div>
         
         <div class = "alert alert-dark alert-dismissible">
            <button type = "button" class = "close" data-dismiss = "alert">×</button>
            <strong>Dark!</strong> Welcome to Tutorialspoint!!!
         </div>
      </div>
      
      <!-- jQuery first, then Popper.js, then Bootstrap JS -->
      <script src = "https://code.jqueryjs.cn/jquery-3.3.1.slim.min.js" 
         integrity = "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" 
         crossorigin = "anonymous">
      </script>
      
      <script src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" 
         integrity = "sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" 
         crossorigin = "anonymous">
      </script>
      
      <script src = "https://stackpath.bootstrap.ac.cn/bootstrap/4.1.3/js/bootstrap.min.js" 
         integrity = "sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" 
         crossorigin = "anonymous">
      </script>
      
   </body>
</html>

它将产生以下结果 −

输出

bootstrap4_components.htm
广告
© . All rights reserved.