2013-03-17 44 views
0

上午想方設法保住了jQuery的切換狀態,我無法設置Cookie爲我不使用.toggle()努力保護切換狀態與JS

我的代碼

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('-'); 
     } 
    }); 
}); 

Demo

回答

0

你只那裏需要一個children()。因爲你與+文本跨越是點擊<span>(只是一個跨度的孩子)內..

$(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('-'); 
    } 
    }); 
}); 

fiddle here

+0

謝謝你,但切換狀態時不會保留刷新 – 2013-03-17 17:53:57