var bootstrapButton = $.fn.button.noConflict() // return $.fn.button to previously assigned value
$.fn.bootstrapBtn = bootstrapButton // give $().bootstrapBtn the Bootstrap functionality
<buttontype="button"class="btn btn-primary"data-toggle="modal"data-target="#exampleModal"data-whatever="@mdo">Open modal for @mdo</button><buttontype="button"class="btn btn-primary"data-toggle="modal"data-target="#exampleModal"data-whatever="@fat">Open modal for @fat</button><buttontype="button"class="btn btn-primary"data-toggle="modal"data-target="#exampleModal"data-whatever="@getbootstrap">Open modal for @getbootstrap</button>
...more buttons...
<divclass="modal fade"id="exampleModal"tabindex="-1"role="dialog"aria-labelledby="exampleModalLabel"><divclass="modal-dialog"role="document"><divclass="modal-content"><divclass="modal-header"><buttontype="button"class="close"data-dismiss="modal"aria-label="Close"><spanaria-hidden="true">×</span></button><h4class="modal-title"id="exampleModalLabel">New message</h4></div><divclass="modal-body"><form><divclass="form-group"><labelfor="recipient-name"class="control-label">Recipient:</label><inputtype="text"class="form-control"id="recipient-name"></div><divclass="form-group"><labelfor="message-text"class="control-label">Message:</label><textareaclass="form-control"id="message-text"></textarea></div></form></div><divclass="modal-footer"><buttontype="button"class="btn btn-default"data-dismiss="modal">Close</button><buttontype="button"class="btn btn-primary">Send message</button></div></div></div></div>
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var recipient = button.data('whatever') // Extract info from data-* attributes
// If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
// Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
var modal = $(this)
modal.find('.modal-title').text('New message to ' + recipient)
modal.find('.modal-body input').val(recipient)
})
3.7 通过JavaScript处理模态框
打开
1
$('#myModal').modal(options)
参数
名称
类型
默认值
描述
backdrop
boolean 或 字符串 'static'
true
I指定一个静态的背景,当用户点击模态框外部时不会关闭模态框。
keyboard
boolean
true
键盘上的 esc 键被按下时关闭模态框。
show
boolean
true
模态框初始化之后就立即显示出来。
remote
path
false
This option is deprecated since v3.3.0 and has been removed in v4. We recommend instead using client-side templating or a data binding framework, or calling jQuery.loadyourself.如果提供的是 URL,将利用 jQuery 的 load 方法从此 URL 地址加载要展示的内容(只加载一次)并插入 .modal-content 内。如果使用的是 data 属性 API,还可以利用 href 属性指定内容来源地址。下面是一个实例:<a data-toggle="modal" href="remote.html" data-target="#modal">Click me</a>
<divclass="tab-content"><divrole="tabpanel"class="tab-pane fade in active"id="home">...</div><divrole="tabpanel"class="tab-pane fade"id="profile">...</div><divrole="tabpanel"class="tab-pane fade"id="messages">...</div><divrole="tabpanel"class="tab-pane fade"id="settings">...</div></div>
6.3 JavaScript调用
12345678
$('#myTabs a').click(function (e) {
e.preventDefault()
$(this).tab('show')
})
$('#myTabs a[href="#profile"]').tab('show') // Select tab by name
$('#myTabs a:first').tab('show') // Select first tab
$('#myTabs a:last').tab('show') // Select last tab
$('#myTabs li:eq(2) a').tab('show') // Select third tab (0-indexed)
方法
$().tab
该方法可以激活标签页元素和内容容器。标签页需要用一个 data-target 或者一个指向 DOM 中容器节点的 href。
.tab('show')
Selects the given tab and shows its associated content. Any other tab that was previously selected becomes unselected and its associated content is hidden. Returns to the caller before the tab pane has actually been shown (i.e. before the shown.bs.tabevent occurs).
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
e.target// newly activated tab
e.relatedTarget// previous active tab
})
7 工具提示 tooltips.js
7.1 基本使用
1
2
3
4
5
6
7
<buttontype="button"class="btn btn-default"data-toggle="tooltip"data-placement="left"title="Tooltip on left">Tooltip on left</button><buttontype="button"class="btn btn-default"data-toggle="tooltip"data-placement="top"title="Tooltip on top">Tooltip on top</button><buttontype="button"class="btn btn-default"data-toggle="tooltip"data-placement="bottom"title="Tooltip on bottom">Tooltip on bottom</button><buttontype="button"class="btn btn-default"data-toggle="tooltip"data-placement="right"title="Tooltip on right">Tooltip on right</button>
7.2 JavaScript调用
1
$('#example').tooltip(options)
参数
选项名称
类型/默认值
Data 属性名称
描述
animation
boolean 默认值:true
data-animation
提示工具使用 CSS 渐变滤镜效果。
html
boolean 默认值:false
data-html
向提示工具插入 HTML。如果为 false,jQuery 的 text 方法将被用于向 dom 插入内容。如果您担心 XSS 攻击,请使用 text。
placement
string|function 默认值:top
data-placement
规定如何定位提示工具(即 top|bottom|left|right|auto)。 当指定为 auto 时,会动态调整提示工具。例如,如果 placement 是 “auto left”,提示工具将会尽可能显示在左边,在情况不允许的情况下它才会显示在右边。
$('#myTooltip').on('show.bs.tooltip', function () { // 执行一些动作... })
shown.bs.tooltip
当提示工具对用户可见时触发该事件(将等待 CSS 过渡效果完成)。
$('#myTooltip').on('shown.bs.tooltip', function () { // 执行一些动作... })
hide.bs.tooltip
当调用 hide 实例方法时立即触发该事件。
$('#myTooltip').on('hide.bs.tooltip', function () { // 执行一些动作... })
hidden.bs.tooltip
当提示工具对用户隐藏时触发该事件(将等待 CSS 过渡效果完成)。
$('#myTooltip').on('hidden.bs.tooltip', function () { // 执行一些动作... })
123
$('#myTooltip').on('hidden.bs.tooltip', function () {
// do something…
})
8 弹出框 popover.js
8.1 基本使用
基本
1
<buttontype="button"class="btn btn-lg btn-danger"data-toggle="popover"title="Popover title"data-content="And here's some amazing content. It's very engaging. Right?">点我弹出/隐藏弹出框</button>
弹出方向
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<buttontype="button"class="btn btn-default"data-container="body"data-toggle="popover"data-placement="left"data-content="Vivamus sagittis lacus vel augue laoreet rutrum faucibus.">
Popover on 左侧
</button><buttontype="button"class="btn btn-default"data-container="body"data-toggle="popover"data-placement="top"data-content="Vivamus sagittis lacus vel augue laoreet rutrum faucibus.">
Popover on 顶部
</button><buttontype="button"class="btn btn-default"data-container="body"data-toggle="popover"data-placement="bottom"data-content="Vivamus
sagittis lacus vel augue laoreet rutrum faucibus.">
Popover on 底部
</button><buttontype="button"class="btn btn-default"data-container="body"data-toggle="popover"data-placement="right"data-content="Vivamus sagittis lacus vel augue laoreet rutrum faucibus.">
Popover on 右侧
</button>
让警告框监听具有 data-dismiss="alert" 属性的后裔元素的点击(click)事件。(如果是通过 data 属性进行的初始化则无需使用)
$().alert('close')
关闭警告框并从 DOM 中将其删除。如果警告框被赋予了 .fade 和 .in 类,那么,警告框在淡出之后才会被删除。
事件
Bootstrap 的警告框插件对外暴露了一些可以被监听的事件。
事件类型
描述
close.bs.alert
当 close 方法被调用后立即触发此事件。
closed.bs.alert
当警告框被关闭后(也即 CSS 过渡效果完毕之后)立即触发此事件。
1
2
3
$('#myAlert').on('closed.bs.alert', function () {
// do something…
})
10 按钮 button.js
10.1 加载状态
1
2
3
4
5
6
7
8
9
10
11
<buttontype="button"id="myButton"data-loading-text="Loading..."class="btn btn-primary"autocomplete="off">
Loading state
</button><script>
$('#myButton').on('click', function () {
var $btn = $(this).button('loading')
// business logic...
$btn.button('reset')
})
</script>
10.2 独立的按钮切换状态
1
2
3
<buttontype="button"class="btn btn-primary"data-toggle="button"aria-pressed="false"autocomplete="off">
Single toggle
</button>
<buttontype="button"id="myStateButton"data-complete-text="finished!"class="btn btn-primary"autocomplete="off">
...
</button><script>
$('#myStateButton').on('click', function () {
$(this).button('complete') // button text will be "finished!"
})
</script>
11 折叠 collapse.js
11.1 基本使用
1
2
3
4
5
6
7
8
9
10
11
<aclass="btn btn-primary"role="button"data-toggle="collapse"href="#collapseExample"aria-expanded="false"aria-controls="collapseExample">
Link with href
</a><buttonclass="btn btn-primary"type="button"data-toggle="collapse"data-target="#collapseExample"aria-expanded="false"aria-controls="collapseExample">
Button with data-target
</button><divclass="collapse"id="collapseExample"><divclass="well">
...
</div></div>
<divclass="panel-group"id="accordion"role="tablist"aria-multiselectable="true"><divclass="panel panel-default"><divclass="panel-heading"role="tab"id="headingOne"><h4class="panel-title"><arole="button"data-toggle="collapse"data-parent="#accordion"href="#collapseOne"aria-expanded="true"aria-controls="collapseOne">
Collapsible Group Item #1
</a></h4></div><divid="collapseOne"class="panel-collapse collapse in"role="tabpanel"aria-labelledby="headingOne"><divclass="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</div></div></div><divclass="panel panel-default"><divclass="panel-heading"role="tab"id="headingTwo"><h4class="panel-title"><aclass="collapsed"role="button"data-toggle="collapse"data-parent="#accordion"href="#collapseTwo"aria-expanded="false"aria-controls="collapseTwo">
Collapsible Group Item #2
</a></h4></div><divid="collapseTwo"class="panel-collapse collapse"role="tabpanel"aria-labelledby="headingTwo">
<divclass="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</div></div></div>
<divclass="panel panel-default">
<divclass="panel-heading"role="tab"id="headingThree">
<h4class="panel-title">
<aclass="collapsed"role="button"data-toggle="collapse"data-parent="#accordion"href="#collapseThree"aria-expanded="false"aria-controls="collapseThree">
Collapsible Group Item #3
</a></h4></div>
<divid="collapseThree"class="panel-collapse collapse"role="tabpanel"aria-labelledby="headingThree">
<divclass="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</div></div></div></div>
It’s also possible to swap out .panel-bodys with .list-groups.
$('#identifier').on('slide.bs.carousel', function () { // 执行一些动作... })
slid.bs.carousel
当轮播完成幻灯片过渡效果时触发该事件。
$('#identifier').on('slid.bs.carousel', function () { // 执行一些动作... })
123
$('#myCarousel').on('slide.bs.carousel', function () {
// do something…
})
13 附加 affix.js
12.1 基本使用
To easily add affix behavior to any element, just add data-spy="affix" to the element you want to spy on. Use offsets to define when to toggle the pinning of an element.
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-offset-top="200".
Name
type
default
description
offset
number | function | object
10
Pixels to offset from screen when calculating position of scroll. If a single number is provided, the offset will be applied in both top and bottom directions. To provide a unique, bottom and top offset just provide an object offset: { top: 10 } or offset: { top: 10, bottom: 5 }. Use a function when you need to dynamically calculate an offset.
target
selector | node | jQuery element
the windowobject
Specifies the target element of the affix.
方法
$().affix(options)
Activates your content as affixed content. Accepts an optional options object.
1
2
3
$('#myAffix').affix({
offset: 15
})
** $().affix('checkPosition')**
Recalculates the state of the affix based on the dimensions, position, and scroll position of the relevant elements. The .affix, .affix-top, and .affix-bottom classes are added to or removed from the affixed content according to the new state. This method needs to be called whenever the dimensions of the affixed content or the target element are changed, to ensure correct positioning of the affixed content.
1
$('#myAffix').affix('checkPosition')
事件
Bootstrap’s affix plugin exposes a few events for hooking into affix functionality.
Event Type
Description
affix.bs.affix
This event fires immediately before the element has been affixed.
affixed.bs.affix
This event is fired after the element has been affixed.
affix-top.bs.affix
This event fires immediately before the element has been affixed-top.
affixed-top.bs.affix
This event is fired after the element has been affixed-top.
affix-bottom.bs.affix
This event fires immediately before the element has been affixed-bottom.
affixed-bottom.bs.affix
This event is fired after the element has been affixed-bottom.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构