若是您想要單獨引用該插件的功能,那麼您須要引用 modal.js。或者,正如 Bootstrap 插件概覽 一章中所提到,您能夠引用 bootstrap.js 或壓縮版的 bootstrap.min.js。
您能夠切換模態框(Modal)插件的隱藏內容:css
$('#identifier').modal(options)
一個靜態的模態窗口實例,以下面的實例所示:html
<!DOCTYPE html> <html> <head> <title>Bootstrap 實例 - 模態框(Modal)插件</title> <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <script src="/scripts/jquery.min.js"></script> <script src="/bootstrap/js/bootstrap.min.js"></script> </head> <body> <h2>建立模態框(Modal)</h2> <!-- 按鈕觸發模態框 --> <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> 開始演示模態框 </button> <!-- 模態框(Modal) --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> × </button> <h4 class="modal-title" id="myModalLabel"> 模態框(Modal)標題 </h4> </div> <div class="modal-body"> 在這裏添加一些文本 </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">關閉 </button> <button type="button" class="btn btn-primary"> 提交更改 </button> </div> </div><!-- /.modal-content --> </div><!-- /.modal --> </body> </html>
結果以下所示:jquery
代碼講解:bootstrap
有一些選項能夠用來定製模態窗口(Modal Window)的外觀和感觀,它們是經過 data 屬性或 JavaScript 來傳遞的。下表列出了這些選項:網絡
選項名稱 | 類型/默認值 | Data 屬性名稱 | 描述 |
---|---|---|---|
backdrop | boolean 或 string 'static' 默認值:true |
data-backdrop | 指定一個靜態的背景,當用戶點擊模態框外部時不會關閉模態框。 |
keyboard | boolean 默認值:true |
data-keyboard | 當按下 escape 鍵時關閉模態框,設置爲 false 時則按鍵無效。 |
show | boolean 默認值:true |
data-show | 當初始化時顯示模態框。 |
remote | path 默認值:false |
data-remote | 使用 jQuery .load 方法,爲模態框的主體注入內容。若是添加了一個帶有有效 URL 的 href,則會加載其中的內容。以下面的實例所示: <a data-toggle="modal" href="remote.html" data-target="#modal">請點擊我</a> |
下面是一些可與 modal() 一塊兒使用的有用的方法。ide
方法 | 描述 | 實例 |
---|---|---|
Options: .modal(options) | 把內容做爲模態框激活。接受一個可選的選項對象。 | $('#identifier').modal({ keyboard: false }) |
Toggle: .modal('toggle') | 手動切換模態框。 | $('#identifier').modal('toggle') |
Show: .modal('show') | 手動打開模態框。 | $('#identifier').modal('show') |
Hide: .modal('hide') | 手動隱藏模態框。 | $('#identifier').modal('hide') |
下面的實例演示了方法的用法:函數
<!DOCTYPE html> <html> <head> <title>Bootstrap 實例 - 模態框(Modal)插件方法</title> <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <script src="/scripts/jquery.min.js"></script> <script src="/bootstrap/js/bootstrap.min.js"></script> </head> <body> <h2>模態框(Modal)插件方法</h2> <!-- 按鈕觸發模態框 --> <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> 開始演示模態框 </button> <!-- 模態框(Modal) --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">× </button> <h4 class="modal-title" id="myModalLabel"> 模態框(Modal)標題 </h4> </div> <div class="modal-body"> 按下 ESC 按鈕退出。 </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">關閉 </button> <button type="button" class="btn btn-primary"> 提交更改 </button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal --> <script> $(function () { $('#myModal').modal({ keyboard: true })}); </script> </body> </html>
結果以下所示:工具
只須要點擊 ESC 鍵,模態窗口即會退出。post
下表列出了模態框中要用到事件。這些事件可在函數中當鉤子使用。字體
事件 | 描述 | 實例 |
---|---|---|
show.bs.modal | 在調用 show 方法後觸發。 | $('#identifier').on('show.bs.modal', function () { // 執行一些動做... }) |
shown.bs.modal | 當模態框對用戶可見時觸發(將等待 CSS 過渡效果完成)。 | $('#identifier').on('shown.bs.modal', function () { // 執行一些動做... }) |
hide.bs.modal | 當調用 hide 實例方法時觸發。 | $('#identifier').on('hide.bs.modal', function () { // 執行一些動做... }) |
hidden.bs.modal | 當模態框徹底對用戶隱藏時觸發。 | $('#identifier').on('hidden.bs.modal', function () { // 執行一些動做... }) |
下面的實例演示了事件的用法:
<!DOCTYPE html> <html> <head> <title>Bootstrap 實例 - 模態框(Modal)插件事件</title> <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <script src="/scripts/jquery.min.js"></script> <script src="/bootstrap/js/bootstrap.min.js"></script> </head> <body> <h2>模態框(Modal)插件事件</h2> <!-- 按鈕觸發模態框 --> <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> 開始演示模態框 </button> <!-- 模態框(Modal) --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">× </button> <h4 class="modal-title" id="myModalLabel"> 模態框(Modal)標題 </h4> </div> <div class="modal-body"> 點擊關閉按鈕檢查事件功能。 </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal"> 關閉 </button> <button type="button" class="btn btn-primary"> 提交更改 </button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal --> <script> $(function () { $('#myModal').modal('hide')})}); </script> <script> $(function () { $('#myModal').on('hide.bs.modal', function () { alert('嘿,我據說您喜歡模態框...');}) }); </script> </body> </html>
結果以下所示:
正如上面實例所示,若是您點擊了 關閉 按鈕,即 hide 事件,則會顯示一個警告消息。