站点引用 Bootstrap 插件的方式有两种:
- 单独引用:使用 Bootstrap 的个别的 *.js 文件。一些插件和 CSS
组件依赖于其他插件。如果您单独引用插件,请先确保弄请这些插件之间的依赖关系。 - 编译(同时)引用:使用 bootstrap.js 或压缩版的 bootstrap.min.js。
不要尝试同时引用这两个文件,因为 bootstrap.js 和 bootstrap.min.js 都包含了所有的插件。
所有的插件依赖于 jQuery。所以必须在插件文件之前引用 jQuery
data 属性
- 你可以仅仅通过 data 属性 API 就能使用所有的 Bootstrap 插件,无需写一行 JavaScript 代码。这是
Bootstrap 中的一等 API,也应该是你的首选方式。 - 话又说回来,在某些情况下可能需要将此功能关闭。因此,我们还提供了关闭 data 属性 API 的方法,即解除以 data-api
为命名空间并绑定在文档上的事件。就像下面这样:
<code class="hljs coffeescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">document</span>).<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">off</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'.data-api'</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
- 如需关闭一个特定的插件,只需要在 data-api 命名空间前加上该插件的名称作为命名空间即可,如下所示:
<code class="hljs coffeescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">document</span>).<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">off</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'.alert.data-api'</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
编程方式的 API
我们为所有 Bootstrap 插件提供了纯 JavaScript 方式的 API。所有公开的 API 都是支持单独或链式调用方式,并且返回其所操作的元素集合(注:和jQuery的调用形式一致)。例如:
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">".btn.danger"</span>)<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.button</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"toggle"</span>)<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.addClass</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"fat"</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
所有的方法都可以接受一个可选的选项对象作为参数,或者一个代表特定方法的字符串,或者不带任何参数(这种情况下,将会初始化插件为默认行为),如下所示:
<code class="hljs javascript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 初始化为默认行为</span> $(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"#myModal"</span>).modal() <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 初始化为不支持键盘 </span> $(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"#myModal"</span>).modal({ keyboard: <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">false</span> }) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 初始化并立即调用 show</span> $(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"#myModal"</span>).modal(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'show'</span>) </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
每个插件在 Constructor 属性上也暴露了其原始的构造函数:$.fn.popover.Constructor。如果您想获取某个特定插件的实例,可以直接通过页面元素获取:
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$(</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'[rel=popover]'</span>).data(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'popover'</span>).</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
避免命名空间冲突
某些时候 Bootstrap 插件可能需要与其他 UI 框架一起使用。在这种情况下,可能会发生命名空间冲突。如果不幸发生了这种情况,你可以通过调用插件的 .noConflict 方法恢复其原始值。
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">// 返回 $<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.fn</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.button</span> 之前所赋的值 var bootstrapButton = $<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.fn</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.button</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.noConflict</span>() // 为 $()<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bootstrapBtn</span> 赋予 Bootstrap 功能 $<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.fn</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bootstrapBtn</span> = bootstrapButton </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
事件
Bootstrap 为大多数插件的独特行为提供了自定义事件。一般来说,这些事件有两种形式:
- 动词不定式:这会在事件开始时被触发。例如 ex: show。动词不定式事件提供了 preventDefault
功能。这使得在事件开始前可以停止操作的执行。
<code class="hljs javascript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'#myModal'</span>).on(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'show.bs.modal'</span>, <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(e)</span> {</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 阻止模态框的显示</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!data) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> e.preventDefault() })</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
- 过去分词形式:这会在动作执行完毕之后被触发。例如 ex: shown。