2011-09-21 64 views
0

這裏是我的代碼:設置cookie來記住下拉菜單狀態 - JQuery的

$(document).ready(function() { 
    $("#root ul").each(function() {$(this).css("display", "none");}); 
    $("#root .category").click(function() { 
     var childid = "#" + $(this).attr("childid"); 
     if ($(childid).css("display") == "none")  { 
      $(childid).css("display", "block"); 
     } 
     else { 
      $(childid).css("display", "none"); 
     } 
     if ($(this).hasClass("cat_close")) { 
      $(this).removeClass("cat_close").addClass("cat_open");} 
     else { 
      $(this).removeClass("cat_open").addClass("cat_close"); 
     } 
    }); 
}); 

你能不能幫我在創建/設置,會記得離開頁面或後打開菜單框一個cookie點擊鏈接?順便說一句,我想用Jquery的cookie插件。

謝謝!!

回答

4

使用jquery-cookie plugin

你的代碼將看起來像(如果我理解正確的是這裏):

我對你的好男人
<script type="text/javascript"> 
    jQuery(document).ready(function() { 

     var selectedId = jQuery.cookie('selected-sub-menu-id'); // get selected submenu id 
     jQuery("#root ul").each(function() { 
      if (jQuery(this).attr('id') != selectedId) { 
       jQuery(this).css("display", "none"); 
      } 
     }); 
     jQuery("#root .category").click(function() { 
      var childid = "#" + jQuery(this).attr("childid"); 

      jQuery(childid).toggle(); 

      if (jQuery(this).hasClass("cat_close")) { 
       jQuery(this).removeClass("cat_close").addClass("cat_open"); 
      } 
      else { 
       jQuery(this).removeClass("cat_open").addClass("cat_close"); 
      } 
      jQuery.cookie('selected-sub-menu-id', childid.substring(1)); // set selected submenu id 
     }); 

    }); 
</script> 
+1

一個字:輝煌!工作就像一個魅力..謝謝你! – John