2014-06-05 288 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迴應:) –