Bootstrap 按钮(Button)插件

前言 🔗

  • 按钮(Button)在 Bootstrap 按钮 一章中介绍过。通过按钮(Button)插件,可以添加进一些交互,比如控制按钮状态,或者为其他组件(如工具栏)创建按钮组。

1、加载状态

  • 如需向按钮添加加载状态,只需要简单地向 button 元素添加 data-loading-text="Loading..." 作为其属性即可,如下面实例所示:

  • 实例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <button id="fat-btn" class="btn btn-primary" data-loading-text="Loading..." type="button">加载状态</button>

    <script>
    $(function () {
    $(".btn").click(function () {
    $(this).button('loading').delay(1000).queue(function () {
    // $(this).button('reset');
    // $(this).dequeue();
    });
    });
    });
    </script>
  • 效果 🔗

2、单个切换

  • 如需激活单个按钮的切换(即改变按钮的正常状态为按压状态,反之亦然),只需向 button 元素添加 data-toggle="button" 作为其属性即可,如下面实例所示:

  • 实例

    1
    <button type="button" class="btn btn-primary" data-toggle="button">单个切换</button>
  • 效果 🔗

3、复选框

  • 可以创建复选框组,并通过向 btn-group 添加 data 属性 data-toggle="buttons" 来添加复选框组的切换。

  • 实例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <div class="btn-group" data-toggle="buttons">
    <label class="btn btn-primary">
    <input type="checkbox"> 选项 1
    </label>
    <label class="btn btn-primary">
    <input type="checkbox"> 选项 2
    </label>
    <label class="btn btn-primary">
    <input type="checkbox"> 选项 3
    </label>
    </div>
  • 效果 🔗

4、单选按钮

  • 类似地,您可以创建单选按钮组,并通过向 btn-group 添加 data 属性 data-toggle="buttons" 来添加单选按钮组的切换。

  • 实例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <div class="btn-group" data-toggle="buttons">
    <label class="btn btn-primary">
    <input type="radio" name="options" id="option1"> 选项 1
    </label>
    <label class="btn btn-primary">
    <input type="radio" name="options" id="option2"> 选项 2
    </label>
    <label class="btn btn-primary">
    <input type="radio" name="options" id="option3"> 选项 3
    </label>
    </div>
  • 效果 🔗

5、用法

  • 可以 通过 JavaScript 启用按钮(Button)插件,如下所示:

    1
    $('.btn').button()

6、方法

  • 下面是一些按钮(Button)插件中有用的方法:
方法 描述 实例
button(‘toggle’) 切换按压状态。赋予按钮被激活的外观。您可以使用 data-toggle 属性启用按钮的自动切换 $().button('toggle')
.button(‘loading’) 当加载时,按钮是禁用的,且文本变为 button 元素的 data-loading-text 属性的值 $().button('loading')
.button(‘reset’) 重置按钮状态,文本内容恢复为最初的内容。当您想要把按钮返回为原始的状态时,该方法非常有用 $().button('reset')
.button(string) 该方法中的字符串是指由用户声明的任何字符串。使用该方法,重置按钮状态,并添加新的内容 $().button(string)
  • 下面的实例演示了上面方法的用法:

  • 实例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    <h4>演示 .button('toggle') 方法</h4>
    <div id="myButtons1" class="bs-example">
    <button type="button" class="btn btn-primary">原始</button>
    </div>

    <h4>演示 .button('loading') 方法</h4>
    <div id="myButtons2" class="bs-example">
    <button type="button" class="btn btn-primary" data-loading-text="Loading...">原始</button>
    </div>

    <h4>演示 .button('reset') 方法</h4>
    <div id="myButtons3" class="bs-example">
    <button type="button" class="btn btn-primary" data-loading-text="Loading...">原始</button>
    </div>

    <h4>演示 .button(string) 方法</h4>
    <button type="button" class="btn btn-primary" id="myButton4" data-complete-text="Loading finished">请点击我</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>
  • 效果 🔗

7、Bootstrap CSS 按钮

8、Bootstrap 布局组件 按钮组

文章目录
  1. 1. 前言 🔗
  2. 2. 1、加载状态
  3. 3. 2、单个切换
  4. 4. 3、复选框
  5. 5. 4、单选按钮
  6. 6. 5、用法
  7. 7. 6、方法
  8. 8. 7、Bootstrap CSS 按钮
  9. 9. 8、Bootstrap 布局组件 按钮组
隐藏目录