我试图按照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