- Bootstrap 教程
- Bootstrap - 首頁
- Bootstrap - 概述
- Bootstrap - 環境設定
- Bootstrap - 從右到左 (RTL)
- Bootstrap - CSS 變數
- Bootstrap - 色彩模式
- Bootstrap 佈局
- Bootstrap - 斷點
- Bootstrap - 容器
- Bootstrap - 網格系統
- Bootstrap - 列
- Bootstrap - 溝槽
- Bootstrap - 實用工具
- Bootstrap - CSS 網格
- Bootstrap 元件
- Bootstrap - 手風琴
- Bootstrap - 警示框
- Bootstrap - 徽章
- Bootstrap - 麵包屑
- Bootstrap - 按鈕
- Bootstrap - 按鈕組
- Bootstrap - 卡片
- Bootstrap - 走馬燈
- Bootstrap - 關閉按鈕
- Bootstrap - 摺疊
- Bootstrap - 下拉選單
- Bootstrap - 列表組
- Bootstrap - 模態框
- Bootstrap - 導航欄
- Bootstrap - 導航和選項卡
- Bootstrap - 側邊欄
- Bootstrap - 分頁
- Bootstrap - 佔位符
- Bootstrap - 氣泡提示
- Bootstrap - 進度條
- Bootstrap - 滾動偵聽
- Bootstrap - 載入動畫
- Bootstrap - 提示框
- Bootstrap - 工具提示
- Bootstrap 表單
- Bootstrap - 表單
- Bootstrap - 表單控制元件
- Bootstrap - 選擇框
- Bootstrap - 複選框和單選按鈕
- Bootstrap - 範圍滑塊
- Bootstrap - 輸入組
- Bootstrap - 浮動標籤
- Bootstrap - 佈局
- Bootstrap - 驗證
- Bootstrap 輔助工具
- Bootstrap - 清除浮動
- Bootstrap - 顏色和背景
- Bootstrap - 彩色連結
- Bootstrap - 焦點環
- Bootstrap - 圖示連結
- Bootstrap - 定位
- Bootstrap - 寬高比
- Bootstrap - 堆疊
- Bootstrap - 拉伸連結
- Bootstrap - 文字截斷
- Bootstrap - 垂直線
- Bootstrap - 視覺隱藏
- Bootstrap 實用工具
- Bootstrap - 背景
- Bootstrap - 邊框
- Bootstrap - 顏色
- Bootstrap - 顯示
- Bootstrap - Flexbox
- Bootstrap - 浮動
- Bootstrap - 互動
- Bootstrap - 連結
- Bootstrap - 物件適應
- Bootstrap - 不透明度
- Bootstrap - 溢位
- Bootstrap - 定位
- Bootstrap - 陰影
- Bootstrap - 尺寸
- Bootstrap - 間距
- Bootstrap - 文字
- Bootstrap - 垂直對齊
- Bootstrap - 可見性
- Bootstrap 演示
- Bootstrap - 網格演示
- Bootstrap - 按鈕演示
- Bootstrap - 導航演示
- Bootstrap - 部落格演示
- Bootstrap - 滑塊演示
- Bootstrap - 走馬燈演示
- Bootstrap - 標題演示
- Bootstrap - 頁尾演示
- Bootstrap - 英雄區域演示
- Bootstrap - 特色區域演示
- Bootstrap - 側邊欄演示
- Bootstrap - 下拉選單演示
- Bootstrap - 列表組演示
- Bootstrap - 模態框演示
- Bootstrap - 徽章演示
- Bootstrap - 麵包屑演示
- Bootstrap - 超大標題演示
- Bootstrap - 粘性頁尾演示
- Bootstrap - 相簿演示
- Bootstrap - 登入演示
- Bootstrap - 定價演示
- Bootstrap - 結賬演示
- Bootstrap - 產品演示
- Bootstrap - 封面演示
- Bootstrap - 儀表盤演示
- Bootstrap - 粘性頁尾導航欄演示
- Bootstrap - 砌體佈局演示
- Bootstrap - 啟動模板演示
- Bootstrap - 相簿 RTL 演示
- Bootstrap - 結賬 RTL 演示
- Bootstrap - 走馬燈 RTL 演示
- Bootstrap - 部落格 RTL 演示
- Bootstrap - 儀表盤 RTL 演示
- Bootstrap 有用資源
- Bootstrap - 常見問題解答
- Bootstrap - 快速指南
- Bootstrap - 有用資源
- Bootstrap - 討論
Bootstrap - 按鈕外掛
按鈕在章節Bootstrap 按鈕中進行了說明。使用此外掛,您可以新增一些互動,例如控制按鈕狀態或建立按鈕組以用於更多元件(如工具欄)。
如果您想單獨包含此外掛功能,則需要 button.js。否則,如章節Bootstrap 外掛概述中所述,您可以包含 bootstrap.js 或其最小化版本 bootstrap.min.js。
載入狀態
要向按鈕新增載入狀態,只需將 data-loading-text = "Loading..." 作為屬性新增到按鈕元素,如下例所示:
<button id = "fat-btn" class = "btn btn-primary" data-loading-text = "Loading..." type = "button">
Loading state
</button>
<script>
$(function() {
$(".btn").click(function(){
$(this).button('loading').delay(1000).queue(function() {
// $(this).button('reset');
});
});
});
</script>
單擊按鈕時,輸出將如以下影像所示:
單次切換
要在單個按鈕上啟用切換(即,將按鈕的正常狀態更改為按下狀態,反之亦然),請將 data-toggle = "button" 作為屬性新增到按鈕元素,如下例所示:
<button type = "button" class = "btn btn-primary" data-toggle = "button"> Single toggle </button>
複選框
您可以建立一組複選框並向其新增切換功能,只需將資料屬性 data-toggle = "buttons" 新增到 btn-group。
<div class = "btn-group" data-toggle = "buttons">
<label class = "btn btn-primary">
<input type = "checkbox"> Option 1
</label>
<label class = "btn btn-primary">
<input type = "checkbox"> Option 2
</label>
<label class = "btn btn-primary">
<input type = "checkbox"> Option 3
</label>
</div>
單選按鈕
同樣,您可以建立一組單選按鈕並向其新增切換功能,只需將資料屬性 data-toggle = "buttons" 新增到 btn-group。
<div class = "btn-group" data-toggle = "buttons">
<label class = "btn btn-primary">
<input type = "radio" name =" options" id = "option1"> Option 1
</label>
<label class = "btn btn-primary">
<input type = "radio" name = "options" id = "option2"> Option 2
</label>
<label class = "btn btn-primary">
<input type = "radio" name = "options" id = "option3"> Option 3
</label>
</div>
用法
您可以透過以下方式使用 JavaScript啟用按鈕外掛:
$('.btn').button()
選項
沒有選項。
方法
以下是按鈕外掛的一些有用方法:
| 方法 | 描述 | 示例 |
|---|---|---|
button('toggle') |
切換按下狀態。使按鈕看起來像已被啟用。您還可以使用 data-toggle 屬性啟用按鈕的自動切換。 |
$().button('toggle')
|
.button('loading') |
載入時,按鈕將被停用,並且文字將更改為按鈕元素的 data-loading-text 屬性中的選項。 |
$().button('loading')
|
.button('reset') |
重置按鈕狀態,將原始內容恢復到文字。當您需要將按鈕恢復到初始狀態時,此方法很有用。 |
$().button('reset')
|
.button(string) |
此方法中的字串指的是使用者宣告的任何字串。要重置按鈕狀態並引入新內容,請使用此方法。 |
$().button(string) |
示例
以下示例演示了上述方法的使用:
<h2>Click on each of the buttons to see the effects of methods</h2>
<h4>Example to demonstrate .button('toggle') method</h4>
<div id = "myButtons1" class = "bs-example">
<button type = "button" class = "btn btn-primary">Primary</button>
</div>
<h4>Example to demonstrate .button('loading') method</h4>
<div id = "myButtons2" class = "bs-example">
<button type = "button" class = "btn btn-primary" data-loading-text = "Loading...">
Primary
</button>
</div>
<h4>Example to demonstrate .button('reset') method</h4>
<div id = "myButtons3" class = "bs-example">
<button type = "button" class = "btn btn-primary" data-loading-text = "Loading...">
Primary
</button>
</div>
<h4>Example to demonstrate .button(string) method</h4>
<button type = "button" class = "btn btn-primary" id = "myButton4"
data-complete-text = "Loading finished">
Click Me
</button>
<script type = "text/javascript">
$(function () {
$("#myButtons1 .btn").click(function(){
$(this).button('toggle');
});
});
$(function() {
$("#myButtons2 .btn").click(function(){
$(this).button('loading').delay(1000).queue(function() {
});
});
});
$(function() {
$("#myButtons3 .btn").click(function(){
$(this).button('loading').delay(1000).queue(function() {
$(this).button('reset');
});
});
});
$(function() {
$("#myButton4").click(function(){
$(this).button('loading').delay(1000).queue(function() {
$(this).button('complete');
});
});
});
</script>
廣告