简体   繁体   中英

Bootstrap and JavaScript button change icon with background color

Im tried to change icon with background color appear on the on click event but background color not change. any solution for this? here the my code

 jQuery(function($) { $('#swapFire').on('click', function() { var $el = $(this), textNode = this.lastChild; $el.find('span').toggleClass('glyphicon-fire glyphicon-tint'); $el.toggleClass('showFire'); }); });
 .btn { margin: 20px; }
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <button id="swapFire" class="btn btn-primary showFire"> <span class="glyphicon glyphicon-fire"></span> Gimme Fire </button>

Add Toggle class

$el.toggleClass('btn-primary btn-danger');

 jQuery(function($) { $('#swapFire').on('click', function() { var $el = $(this), textNode = this.lastChild; $el.find('span').toggleClass('glyphicon-fire glyphicon-tint'); $el.toggleClass('btn-primary btn-danger'); $el.toggleClass('showFire'); }); });
 .btn { margin: 20px; }
 <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <button id="swapFire" class="btn btn-primary showFire"> <span class="glyphicon glyphicon-fire"></span> Gimme Fire </button>

You have to also toggle btn-primary then it changes the background color

 jQuery(function ($) { $('#swapFire').on('click', function () { var $el = $(this), textNode = this.lastChild; $el.find('span').toggleClass('glyphicon-fire glyphicon-tint'); $el.toggleClass('btn-primary btn-success'); $el.toggleClass('showFire'); }); });
 .btn { margin: 20px; }
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <button id="swapFire" class="btn btn-primary showFire"> <span class="glyphicon glyphicon-fire"></span> Gimme Fire </button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM