我想讓我的菜單系統讀取一個cookie,它可以記住菜單狀態。一旦記住該菜單將保持打開/關閉取決於cookie。jquery菜單記住菜單的狀態
在javascript中使用alert我已經能夠在點擊之前和之後讀取cookie,但很遺憾,我無法獲得if語句正確以便保持打開/關閉隱藏的html。
任何幫助非常感謝!下面
代碼:
HTML
<li class="primary"><a href="user/me">My Profile</a></li>
<div class="menu_content">
<ul>
<li class="secondary"><a href="<?php print base_path() ?>user/my-education">My Education History</a></li>
<li class="secondary"><a href="<?php print base_path() ?>user/my-achievements">My Achievements</a>
<ul>
<li><a href="user/school">School</a></li>
<li><a href="user/my-achievements/year1">Year 1</a></li>
</ul>
</li>
<li class="secondary"><a href="user/my-details">My Account Details</a></li>
<li class="secondary"><a href="user/my-settings">My Account Settings</a></li>
</ul>
</div>
<li class="primary"><a href="compare">Compare</a></li>
<div class="menu_content">
<ul>
<li class="secondary"><a href="user/my-scores">My Scores</a></li>
</ul>
</div>
jQuery的
var state = $.cookie("panelState");
$("li.primary").click(function(){
if($.cookie("panelState") == "expanded") {
$(this).next().slideToggle('300');
$(this).removeClass("open");
$(this).cookie("panelState", "collapsed");
alert($.cookie("panelState"));
} else if ($.cookie("panelState") == "collapsed") {
$(this).next().slideToggle('300');
$(this).addClass("open");
$(this).cookie("panelState", "expanded");
alert($.cookie("panelState"));
}
});
你可能想看看jQuery的'data'方法,而不是使用cookie菜單:http://api.jquery.com/jQuery.data/ – Sarfraz 2010-10-08 12:41:03