2 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個贊
添加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>

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個贊
您還必須切換 btn-primary 然后它會更改背景顏色
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>
添加回答
舉報