4

我的任务是为菜单设置 cookie。我有一个水平菜单。当我点击菜单时,菜单的 id 被设置到 cookie。但是当我选择下一个菜单时,cookie 显示第一个输入值.为此我使用$.removeCookie("activeDivId");了。但它不能正常工作。我该如何解决这个问题?html代码是

 <div class="menuBar">
            <div id="divHome" class="menuHeader ui-corner-top">
                <span><a href="#" onclick="Home()" id="home">Home</a></span>
            </div>
            <div id="divNewTransaction" class="menuHeader ui-corner-top">
                <span><a href="#" onclick="NewTransaction()" >New Transaction</a></span>
            </div>
</div>

javascript文件是

   $(document).ready(function () {
                $(".menuHeader").click(function () { 
                $.removeCookie("activeDivId");
                $.cookie("activeDivId", this.id); });
                alert(document.cookie);
                var activeDivId = $.cookie("activeDivId") ? $.cookie("activeDivId") : "divHome";
                $("#" + activeDivId).addClass("menuHeaderActive");
            });
4

2 回答 2

2
$(document).ready(function () {

    //for debugging purpose, so that you can see what is in the menu cookie
    $('#debug').html('Cookie Content : ' + readCookie('menu'));

    //if cookie menu exists
    if (readCookie('menu')) {

        //loop through the menu item
        $('#menu a').each(function () {
            //match the correct link and add selected class to it
            if ($(this).html() == readCookie('menu')) $(this).addClass('selected');
        }); 
    }

    $('#menu a').click(function () {

        //Set the cookie according to the text in the link
        createCookie('menu', $(this).html(),1);
    });

});


/* Cookie Function */
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}
于 2013-04-08T04:56:37.757 回答
0

您可以使用此方法代替 jquery 方法

         function createCookie(name, value, days) {
          if (days) {

              var date = new Date();
              date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
              var expires = "; expires=" + date.toGMTString();
          }
          else var expires = "";

          //var fixedName = '';
       /// name =name;

          document.cookie = name + "=" + value + expires + "; path=/";
          this[name] = value;
      }

      function readCookie(name) {

          var nameEQ = name + "=";
          var ca = document.cookie.split(';');
          for (var i = 0; i < ca.length; i++) {
              var c = ca[i];
              while (c.charAt(0) == ' ') c = c.substring(1, c.length);
              if (c.indexOf(nameEQ) == 0) 
              return c.substring(nameEQ.length, c.length);

          }
          return c;
      }

      function eraseCookie(cookiename) {
          this.createCookie(cookiename, '', -1);
          this[cookiename] = undefined;
         // Ecookie(cookiename);


           }
于 2013-04-04T08:37:51.297 回答