Style:
Click the second button to eject the callback function. Then change the color.
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Title</title> <Linkrel= "stylesheet"href= "T-switch.css"></Head><Body><spanclass= "Switch Switch-off"name= "S_marketenable"Data-check= "1"ID= "Stwichbtn_1"></span><spanclass= "Switch Switch-off"name= "S_marketenable"Data-check= "1"ID= "Stwichbtn_2"></span><Scriptsrc= "Jquery.min.js"></Script><Scriptsrc= "Switch-button.js"></Script><Script>//callback function functionCallback (DOM) {varID=dom.attr ('ID'); alert (ID); }//take DOM elements based on name $('[name=s_marketenable]'). Switchbutton ({callback:callback});</Script></Body></HTML>
;(function($) {$.fn.switchbutton=function(options) {varDefaults ={target:$ ( This), Callback:NULL }; varopts =$.extend ({},defaults,options); var_self = This; varSWTICHBTN ={init:function () { var_this = This; //if it's more than one button if(Opts.target.length >1) {$.map (Opts.target,function(domele,index) {varid = $ (domele). attr (' id ')); if(!ID) {ID= "STWICHBTN" +index $ (domele). attr (' ID ', id); } $(' # ' + ID). On (' click ',function() {_this.clickevent (Domele); }) }) }Else{$ (_self). On (' Click ',function() {_this.bindevent (opts.target); })}, Clickevent:function(Domele) {var_this = This; if($ (Domele). Hasclass (' switch-on ')) {_this.off ($ (domele)); }Else{_this.on ($ (domele)); }}, Bindevent:function(DOM) {var_this = This; if(Dom.hasclass (' switch-on ') {_this.off (DOM); }Else{_this.on (DOM); }}, on:function(DOM) {Dom.addclass (' switch-on '). Removeclass (' Switch-off ')); Opts.callback&&Opts.callback (DOM); }, off:function(DOM) {Dom.addclass (' Switch-off '). Removeclass (' switch-on ')); Opts.callback&&Opts.callback (DOM); }} swtichbtn.init (opts); }}) (JQuery);
[Class|=switch]{Display:Block;position:relative;width:50px;Height:20px;Background-color:#ffffff;-webkit-border-radius:16px;-moz-border-radius:16px;Border-radius:16px;}. Switch:before{position:Absolute;Display:Block;content:"';width:19px;Height:19px;Border:1px solid #bcbcbc;Background-color:#fff;-webkit-border-radius:50%;-moz-border-radius:50%;Border-radius:50%;cursor:Pointer;}. switch-on{Display:Block;position:relative;width:50px;Height:20px;Background-color:#1a75ff;-webkit-border-radius:16px;-moz-border-radius:16px;Border-radius:16px;}. Switch-on:before{ Left:30px;transition:Background-color 0.4s, left 0.2s;}. Switch-off:before{ Left:0px;transition:Background-color 0.4s, left 0.2s;}. Switch-off{Display:Block;position:relative;width:50px;Height:20px;Background-color:#b0b0b0;-webkit-border-radius:16px;-moz-border-radius:16px;Border-radius:16px;}
jquery button Toggle Plugin