2013-04-04 90 views
4

我有一個任務來設置菜單的Cookie。我有一個水平菜單。當我點擊菜單時,菜單的ID設置爲cookie。但是當我選擇下一個菜單cookie會顯示第一個輸入值。爲此,我使用了$.removeCookie("activeDivId");。但它不能正常工作。如何解決此問題? HTML代碼Cookie沒有正確使用jquery刪除

<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"); 
      }); 
+0

警報($。餅乾( 「activeDivId」)) ;而不是alert(document.cookie); – 2013-04-04 05:33:02

+0

同樣的事情正在發生。第二個點擊它的ok.correct div ID顯示 – Niths 2013-04-04 05:34:47

+0

試試$ .cookie(「activeDivId」,null);而不是$ .removeCookie(「activeDivId」); – 2013-04-04 06:34:21

回答

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


      } 
+0

如何將這個應用於我的代碼? – Niths 2013-04-04 08:48:12