2012-11-16 73 views
3

我見過一些非常類似的問題,但一直沒有找到我正在尋找的答案。我已經確定瞭解決方法,但希望知道執行該任務的正確方法。如何使用jQuery按下按鈕後保持活動

我希望的是點擊按鈕,並保持活動狀態持久。下一次點擊會切換狀態,這是所需的。我真正需要知道的是如何解決uiButton:活動狀態。

HTML:

<input type='button' class='uiButton' id='testbutton' value='testValue'> 

CSS:

.uiButton{ 
    background-color: red; 
} 

.uiButton:active{ 
    background-color:blue; 
} 

的Javascript/jQuery的:

$('.uiButton').click(function() { 
    $(this).toggleClass(//active state); 
}); 

回答

12

您應該創建一個主動類

CSS

.uiButton:active, .active { 
    background-color:blue; 
} 

JS

$('.uiButton').click(function() { 
    $(this).toggleClass("active"); 
}); 
+3

這就是解決方法我現在有。我試圖看看是否有更好的方法。 – pri0ritize

+2

這不是一個解決方法:)這是做它的方式 –

+0

你是對的。我只是希望有一個更優雅的解決方案。如果沒有一個,我會堅持我所擁有的!謝謝您的幫助。 – pri0ritize

0

你想要的按鈕,以保持主動下課後我t的點擊? (不知道,如果你想以允許untoggled(紅色)再次?)..反正...

CSS:

.uiButton { 
    background-color: red; 
} 

.uiButton-active, .uiButton:hover { 
    background-color: blue; 
} 

然後....:

$('.uiButton').unbind('click').click(function() { 
    $(this).attr('class', 'uiButton-active'); 
}); 
0

你不能觸發像:active這樣的css僞選擇器。我知道IST唯一的選擇模擬這種

.uiButtonActive { 
    background-color:blue; 
} 

退房工作JSFIDDLE DEMO

相關問題