更改为默认样式

Changing to default style

本文关键字:样式 默认      更新时间:2023-09-26

代码完成后,按钮的css仍然改变。我想回到正常的css,当按钮是不活动的

JavaScript

$('#btn1').on('click', function() {
  $("#btn1").css({color:'red'})
  $('#story').fadeToggle(400);
});
HTML

<div id="story" style="display:none;">
    My div
</div>
<input id="btn1" type="button" value="Click Here" />

为什么不给这个按钮添加class呢?

$('#btn1').on('click', function(){
    $(this).toggleClass('active'); // If has class, removes it, otherwise, it adds that class
    $('#story').fadeToggle(400);
});

HTML:

<div id="story" style="display:none;">My div</div>
<input id="btn1" type="button" value="Click Here" />
jQuery:

$('#btn1').on('click', function () {
    $(this).toggleClass('active');
    $('#story').fadeToggle(400);
});

一些CSS:

.active {
    color:#f00;
}

工作小提琴:http://jsfiddle.net/7LaeB/

用您想要的更改创建一个css类,并在活动时应用它,在不活动时删除它。例如:

JS:

$('#btn1').on('click', function() {
    $(this).addClass('active')
    $('#story').fadeToggle(400)
});
//then when the button is 'inactive' run removeClass('active')
CSS:

.active{color:red;}