2014-06-05 184 views
0

此代码后,按钮的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" /> 
+1

看看'toggleClass' - http://api.jquery.com/toggleclass/ –

+0

为我们定义了当按钮没有激活时你的意思。即如果你的意思是默认的非活动状态,请使用Joffrey Maheo的回答 – RossBille

回答

0

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/

0

创建所需的更改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;} 
2

为什么不加类到该按钮?

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

ty回应:) –