2 回答
TA贡献1848条经验 获得超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贡献1794条经验 获得超7个赞
您还必须切换 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>
添加回答
举报