Bootstrap 4 可以很容易實現(xiàn)信息提示框。
提示框可以使用 .alert 類, 后面加上 .alert-success, .alert-info, .alert-warning, .alert-danger, .alert-primary, .alert-secondary, .alert-light 或 .alert-dark 類來實現(xiàn):
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>提示框</h2> <p>提示框可以使用 .alert 類, 后面加上指定特定意義的顏色類來實現(xiàn):</p> <div class="alert alert-success"> <strong>成功!</strong> 指定操作成功提示信息。 </div> <div class="alert alert-info"> <strong>信息!</strong> 請注意這個信息。 </div> <div class="alert alert-warning"> <strong>警告!</strong> 設(shè)置警告信息。 </div> <div class="alert alert-danger"> <strong>錯誤!</strong> 失敗的操作 </div> <div class="alert alert-primary"> <strong>首選!</strong> 這是一個重要的操作信息。 </div> <div class="alert alert-secondary"> <strong>次要的!</strong> 顯示一些不重要的信息。 </div> <div class="alert alert-dark"> <strong>深灰色!</strong> 深灰色提示框。 </div> <div class="alert alert-light"> <strong>淺灰色!</strong>淺灰色提示框。 </div> </div> </body> </html>測試看看 ?/?
提示框中在鏈接的標簽上添加 alert-link 類來設(shè)置匹配提示框顏色的鏈接:
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>提示框添加鏈接</h2> <p>提示框中在鏈接的標簽上添加 alert-link 類來設(shè)置匹配提示框顏色的鏈接:</p> <div class="alert alert-success"> <strong>成功!</strong> 你應(yīng)該認真閱讀 <a href="#" class="alert-link">這條信息</a>。 </div> <div class="alert alert-info"> <strong>信息!</strong> 你應(yīng)該認真閱讀 <a href="#" class="alert-link">這條信息</a>。 </div> <div class="alert alert-warning"> <strong>警告!</strong> 你應(yīng)該認真閱讀 <a href="#" class="alert-link">這條信息</a>。 </div> <div class="alert alert-danger"> <strong>錯誤!</strong> 你應(yīng)該認真閱讀 <a href="#" class="alert-link">這條信息</a>。 </div> <div class="alert alert-primary"> <strong>首選!</strong> 你應(yīng)該認真閱讀 <a href="#" class="alert-link">這條信息</a>。 </div> <div class="alert alert-secondary"> <strong>次要的!</strong> 你應(yīng)該認真閱讀 <a href="#" class="alert-link">這條信息</a>。 </div> <div class="alert alert-dark"> <strong>深灰色!</strong>你應(yīng)該認真閱讀 <a href="#" class="alert-link">這條信息</a>。 </div> <div class="alert alert-light"> <strong>灰色!</strong> 你應(yīng)該認真閱讀 <a href="#" class="alert-link">這條信息</a>。 </div> </div> </body> </html>測試看看 ?/?
我們可以在提示框中的 div 中添加 .alert-dismissible 類,然后在關(guān)閉按鈕的鏈接上添加 和 data-dismiss="alert" 類來設(shè)置提示框的關(guān)閉操作。
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.12.5/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>關(guān)閉提示框</h2> <p>我們可以在提示框中的 div 中添加 .alert-dismissible 類,然后在關(guān)閉按鈕的鏈接上添加 class="close" 和 data-dismiss="alert" 類來設(shè)置提示框的關(guān)閉操作。</p> <div class="alert alert-success alert-dismissible"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>成功!</strong> 指定操作成功提示信息。 </div> <div class="alert alert-info alert-dismissible"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>信息!</strong> 請注意這個信息。 </div> <div class="alert alert-warning alert-dismissible"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>警告!</strong> 設(shè)置警告信息。 </div> <div class="alert alert-danger alert-dismissible"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>錯誤!</strong> 失敗的操作。 </div> <div class="alert alert-primary alert-dismissible"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>首選!</strong> 這是一個重要的操作信息。 </div> <div class="alert alert-secondary alert-dismissible"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>次要的!</strong> 顯示一些不重要的信息。 </div> <div class="alert alert-dark alert-dismissible"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>深灰色!</strong> 深灰色提示框。 </div> <div class="alert alert-light alert-dismissible"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>淺灰色!</strong>淺灰色提示框。 </div> </div> </body> </html>測試看看 ?/?
提示: × (×) 是 HTML 實體字符,來表示關(guān)閉操作,而不是字母 "x"。
.fade 和 .show 類用于設(shè)置提示框在關(guān)閉時的淡出和淡入效果:
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.12.5/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>提示框動畫</h2> <p>.fade 和 .show 類用于設(shè)置提示框在關(guān)閉時的淡出和淡入效果:</p> <div class="alert alert-success alert-dismissible fade show"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>成功!</strong> 指定操作成功提示信息。 </div> <div class="alert alert-info alert-dismissible fade show"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>信息!</strong> 請注意這個信息。 </div> <div class="alert alert-warning alert-dismissible fade show"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>警告!</strong> 設(shè)置警告信息。 </div> <div class="alert alert-danger alert-dismissible fade show"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>錯誤!</strong> 失敗的操作。 </div> <div class="alert alert-primary alert-dismissible fade show"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>首選!</strong> 這是一個重要的操作信息。 </div> <div class="alert alert-secondary alert-dismissible fade show"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>次要的!</strong> 顯示一些不重要的信息。 </div> <div class="alert alert-dark alert-dismissible fade show"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>深灰色!</strong> 深灰色提示框。 </div> <div class="alert alert-light alert-dismissible fade show"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>淺灰色!</strong>淺灰色提示框。 </div> </div> </body> </html>測試看看 ?/?