Bootstrap button plug-in


May 04, 2021 09:00 Bootstrap


Table of contents


Bootstrap button plug-in

Button is described in the Bootstrap button chapter. The Button plug-in lets you add interactions such as controlling button status or creating button groups for other components, such as toolbars.

Bootstrap button plug-in If you want to reference the functionality of the plug-in separately, you need to refer to the button .js. Or, as mentioned in the Bootstrap plug-in overview chapter, you can refer to bootstrap.js or the compressed version of Bootstrap.min .js.

The load status

To add a load state to a button, simply add the data-load-text-"Load..." as its property to the button element, as shown in the following example:

<!DOCTYPE html><html><head>
   <title>Bootstrap 实例 - 按钮(Button)插件加载状态</title>
   <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="stylesheet">
   <script src="//cdn.bootcss.com/jquery/2.1.1/jquery.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
   <script src="//cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script></head><body><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');
         });        
      });
   });  </script></body></html>

The result is as follows:

Bootstrap button plug-in

A single switch

To activate the switch of a single button (i.e. to change the normal state of the button to a pressed state, and vice versa), simply add the data-toggle"button" as its property to the button element, as shown in the following example:

<!DOCTYPE html><html><head>
   <title>Bootstrap 实例 - 按钮(Button)插件单个切换</title>
   <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="stylesheet">
   <script src="//cdn.bootcss.com/jquery/2.1.1/jquery.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
   <script src="//cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script></head><body><button type="button" class="btn btn-primary" 
   data-toggle="button"> 单个切换</button></body></html>

The result is as follows:

Bootstrap button plug-in

Check box (Checkbox)

You can create a check box group and add a check box group switch by adding the data property data-toggle="buttons" to btn-group.

<!DOCTYPE html><html><head>
   <title>Bootstrap 实例 - 按钮(Button)插件复选框</title>
   <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="stylesheet">
   <script src="//cdn.bootcss.com/jquery/2.1.1/jquery.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
   <script src="//cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script></head><body><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></body></html>

The result is as follows:

Bootstrap button plug-in

Radio

Similarly, you can create a turntable button group and add a turn of the turntable group by adding the data property data-toggle to btn-group.

<!DOCTYPE html><html><head>
   <title>Bootstrap 实例 - 按钮(Button)插件单选按钮</title>
   <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="stylesheet">
   <script src="//cdn.bootcss.com/jquery/2.1.1/jquery.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
   <script src="//cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script></head><body><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>	</body></html>

The result is as follows:

Bootstrap button plug-in

Usage

You can enable the Button plug-in with JavaScript, as follows:

$('.btn').button()

Options

There are no options.

Method

Here are some useful ways to plug in buttons:

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

Instance

The following example demonstrates the use of the above method:

<!DOCTYPE html><html><head>
   <title>Bootstrap 实例 - 按钮(Button)插件方法</title>
   <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="external nofollow" target="_blank"  rel="stylesheet">
   <script src="//cdn.bootcss.com/jquery/2.1.1/jquery.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script>
   <script src="//cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ></script></head><body><h2>点击每个按钮查看方法效果</h2><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> </body></html>

The result is as follows:

Bootstrap button plug-in