我試圖按照jquery文檔的規範,但我不能初始化按鈕的標籤編程和添加/刪除類。jquery切換按鈕添加刪除類
請看看這個jsbin中的幾行。
http://jsbin.com/akinod/2/edit
HTML
<label for="xAxisToggleBtn"></label>
<input type="checkbox" id="xAxisToggleBtn"/>
CSS
.on {
background-color: green;
color: white;
}
.off {
background-color:brown;
color: white;
}
的Javascript
$(window).load(function() {
$("#xAxisToggleBtn").button();
$('#xAxisToggleBtn').die('click').live('click', function(){
if($(this).is(':checked')){
$(this).button('option', 'label', 'Turn Off');
//why I cannot add/remove classes to change the theme?
$(this).addClass('on');
$(this).removeClass('off');
} else {
$(this).button('option', 'label', 'Turn On');
//why I cannot initialize add/remove classes to change the theme?
$(this).addClass('off');
$(this).removeClass('on');
}
});
});
//why I cannot initialize the label here?
$("#xAxisToggleBtn").button('option', 'label', 'Turn Off');
$("#xAxisToggleBtn").attr('checked', true);
$("#xAxisToggleBtn").addClass('on');
你熟悉的jQuery.toggleClass方法 - http://api.jquery.com/toggleClass/ –
是的,我試過了,沒有工作。他們的功能行爲是相同的。添加/刪除明確做到這一點,所以問題不在於使用toggleClass.Thanks – Gian
你面臨的問題是什麼? – Ashirvad