使用cookie和jquery保存UI状态

时间:2010-12-20 13:47:41

标签: jquery cookies

我正在尝试保存可折叠菜单的状态,并使用jquery cookie插件..我到目前为止遇到了麻烦..但是有什么帮助?

执行折叠/展开的脚本

<script type="text/javascript">
    $(document).ready(function() {

    $(".login-holder > ul > .loginTitle").click(function() {
        $(this).parent().find("li").slideToggle("fast");
        if ($(this).parent().find(".toggle").html() == "+") {
            $(this).parent().find(".toggle").html("-");
        } else {
            $(this).parent().find(".toggle").html("+");
        }
    });
   })
</script>

xhtml

<ul class="account-links">
         <div class="loginTitle">User Options<span class="toggle">-</span></div>
         <div class="menuLinks">
         <li>
            <%= Html.ActionLink<EventController>( x => x.List(), "Events Near Me" )%>
         </li>
         <li>
            <%= Html.ActionLink<MyEventsController>( x => x.List(), "My Events" )%>
         </li>
         <li>
            <%= Html.ActionLink<AccountController>( x => x.Edit(), "My Profile" )%>
         </li>
         <li>
            <%= Html.ActionLink<ClubController>( x => x.List(), "Clubs Near Me" )%>
         </li>
         <li> 
            <%= Html.ActionLink<MyClubsController>( x => x.List(), "My Clubs" )%>
         </li>
         <li>
            <%= Html.ActionLink<AccountController>( x => x.ChangePassword(), "Change My Password" )%>
         </li>
         <li>
            <%= Html.ActionLink<DependantController>( x => x.List(), "My Dependants" ) %>
         </li>
        </div>
      </ul>
   </div>
  <% if ( ViewModel.Profile.HasOrganizerInfo ) { %>
  <div class="login-holder">
     <ul class="account-links">
        <div class="loginTitle">Organizer Details<span class="toggle">-</span></div>
        <div class=menuLinks>
        <li>
           <%= Html.ActionLink<AccountController>( x => x.Organizer(), "Organizer Details" )%>
        </li>
        <li>
           <%= Html.ActionLink<EventController>( x => x.Edit( default(int?) ), "Post An Event" )%>
        </li>
        <li>
           <%= Html.ActionLink<EventAdminController>( x => x.List(), "Events Created By Me" ) %>
        </li>
        <li>
           <%= Html.ActionLink<ClubController>( x => x.Edit( default( int? ) ), "Create A Club" )%>
        </li>
        <li>
           <%= Html.ActionLink<ClubAdminController>( x => x.List( ), "Clubs Created By Me" )%>
        </li>
        </div>
     </ul>
  </div>
  <% } %>

1 个答案:

答案 0 :(得分:0)

我使用cookie完成此操作的一种方法是附加一个设置cookie的unload处理程序 - 它通过查找当前不是默认值的所有类的ID来做到这一点(无论是可见的还是隐藏由你决定),然后将它们保存到cookie中。当再次加载页面时,它将检查该cookie是否存在,如果它存在,它将使用ID字符串作为选择器将toggled from the default类应用于先前保存的项目。