Jquery 试图用JS保留切换状态
我正在尝试保留jQuery的切换状态,无法使用Jquery 试图用JS保留切换状态,jquery,cookies,Jquery,Cookies,我正在尝试保留jQuery的切换状态,无法使用.toggle() 我的代码 jQuery(document).ready(function($) { $("#switch").click(function(event){ if ($("#navbar").css("display")=='block') { $("#navbar").hide('fast'); $(this).removeClass('close').add
.toggle()
我的代码
jQuery(document).ready(function($) {
$("#switch").click(function(event){
if ($("#navbar").css("display")=='block') {
$("#navbar").hide('fast');
$(this).removeClass('close').addClass('open');
$(this).children().children().html('+');
} else {
$("#navbar").show('fast');
$(this).removeClass('open').addClass('close');
$(this).children().children().html('-');
}
});
});
您只需要一个
children()
。由于您使用+
进行span,文本位于单击的
中(只是span的一个子项)
试试这个
jQuery(document).ready(function($) {
$("#switch").click(function(event){
if ($("#navbar").css("display")=='block') {
$("#navbar").hide('fast');
$(this).removeClass('close').addClass('open');
$(this).children().html('+');
} else {
$("#navbar").show('fast');
$(this).removeClass('open').addClass('close');
$(this).children().html('-');
}
});
});
谢谢,但刷新时不会保留切换状态
jQuery(document).ready(function($) {
$("#switch").click(function(event){
if ($("#navbar").css("display")=='block') {
$("#navbar").hide('fast');
$(this).removeClass('close').addClass('open');
$(this).children().html('+');
} else {
$("#navbar").show('fast');
$(this).removeClass('open').addClass('close');
$(this).children().html('-');
}
});
});