利用 Bootstrap 數(shù)據(jù) API(Bootstrap Data API),大部分的插件可以在不編寫任何代碼的情況下被觸發(fā)。
站點(diǎn)引用 Bootstrap 插件的方式有兩種:(推薦學(xué)習(xí):Bootstrap視頻教程)
單獨(dú)引用:使用 Bootstrap 的個別的 *.js 文件。一些插件和 CSS 組件依賴于其他插件。如果您單獨(dú)引用插件,請先確保弄清這些插件之間的依賴關(guān)系。
編譯(同時)引用:使用 bootstrap.js 或壓縮版的 bootstrap.min.js。
不要嘗試同時引用這兩個文件,因?yàn)?bootstrap.js 和 bootstrap.min.js 都包含了所有的插件。
所有的插件依賴于 jQuery。所以必須在插件文件之前引用 jQuery。請?jiān)L問 bower.json 查看 Bootstrap 當(dāng)前支持的 jQuery 版本。
data 屬性
你可以僅僅通過data屬性API就能使用所有的Bootstrap插件,無需寫一行JavaScript代碼。這是Bootstrap中的一等API,也應(yīng)該是你的首選方式。
話又說回來,在某些情況下可能需要將此功能關(guān)閉。因此,我們還提供了關(guān)閉data 屬性API 的方法,即解除以data-api為命名空間并綁定在文檔上的事件。就像下面這樣:
$(document).off('.data-api')
如需關(guān)閉一個特定的插件,只需要在 data-api 命名空間前加上該插件的名稱作為命名空間即可,如下所示:
$(document).off('.alert.data-api')
實(shí)例:模態(tài)框插件
<head> <meta charset="utf-8"> <title>Bootstrap 實(shí)例 - 模態(tài)框(Modal)插件</title> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <h2>創(chuàng)建模態(tài)框(Modal)</h2> <!-- 按鈕觸發(fā)模態(tài)框 --> <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> 開始演示模態(tài)框 </button> <!-- 模態(tài)框(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"> 模態(tài)框(Modal)標(biāo)題 </h4> </div> <div class="modal-body"> 在這里添加一些文本 </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">關(guān)閉 </button> <button type="button" class="btn btn-primary"> 提交更改 </button> </div> </div><!-- /.modal-content --> </div><!-- /.modal --> </div> </body> </html>