一、效果圖
二、代碼實現
<!DOCTYPE html> <html> <head> <!-- 所需js/css文件 --> <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/css/bootstrap3/bootstrap-switch.min.css" rel="stylesheet"> <script src="https://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.3.2/js/bootstrap-switch.min.js"></script> </head> <body> <!-- HTML --> <input type="checkbox" checked id="checkbox"/> <!-- JS --> <script type="text/javascript"> $(function(){ /* 初始化控件 */ $("#checkbox").bootstrapSwitch({ onText : "ON", // 設置ON文本 offText : "OFF", // 設置OFF文本 onColor : "success",// 設置ON文本顏色 (info/success/warning/danger/primary) offColor : "info", // 設置OFF文本顏色 (info/success/warning/danger/primary) size : "normal", // 設置控件大小,從小到大 (mini/small/normal/large) // 當開關狀態改變時觸發 onSwitchChange : function(event, state) { if (state == true) { alert("ON"); } else { alert("OFF"); } } }); }); </script> </body> </html>