2013-04-10 77 views
0

我想隱藏隨機多個元素,所以我已經做了每個唯一的ID。Cookie隱藏多個元素

$(function() { 
    var left= $('#left'), 
     right = $('#right'), 
     heads = $('.maintitle'), 
     i, 
     leftHead = $(left).find(heads), 
     rightHead = $(right).find(heads); 
     $(leftHead).prepend('<div class="close"></div>'); 
     $(rightHead).prepend('<div class="close"></div>'); 
     var close = $('.close'), 
     Lnumber = $('#left .module'), 
     Rnumber =$('#right .module'); 
      for (i=0;i<Lnumber.length;i++) { 
       $(Lnumber)[i].id='widg'+i; 
       } 
     close.click(function() { 
      var id = $(this).parent().parent().attr('id'); 
      $(this).parent().parent().slideUp().addClass('hidden'); 
      SetCookie('hidden'+id,"true",100); 
      }); 
     var cookieId = $('#left .module'); 
      for (i=0;i<cookieId.length;i++) { 
      var newArray = [cookieId[i].id]; 
      if (ReadCookie(newArray) == 'true') { 
       var cName = newArray.replace('hidden',''); 
       alert(cName); 
       } 
      } 
     }); 

正如你可以在ReadCookie函數中看到的那樣,我讓它提醒隱藏的ID,儘管這也不起作用。我是Cookies的新品牌,只是很難讓它正常工作。

下面是餅乾的功能...

function SetCookie(cookieName,cookieValue,nDays) { 
var today = new Date(); 
var expire = new Date(); 
if (nDays==null || nDays==0) nDays=1; 
expire.setTime(today.getTime() + 3600000*24*nDays); 
document.cookie = cookieName+"="+escape(cookieValue) 
    + ";expires="+expire.toGMTString(),';path = /'; 
} 

function KillCookie(cookieName) { 
    SetCookie(cookieName,"", - 1); 
} 

function ReadCookie(cookieName) { 
    var theCookie=""+document.cookie; 
    var ind=theCookie.indexOf(cookieName+"="); 
    if (ind==-1 || cookieName=="") return ""; 
    var ind1=theCookie.indexOf(";",ind); 
    if (ind1==-1) ind1=theCookie.length; 
    return unescape(theCookie.substring(ind+cookieName.length+1,ind1)); 
    } 

如果有人看到任何其他方式來做到這一點是好的,只需要獲得特定的ID的點擊時,以確保他們留隱患和改變頁面。

+0

看這個問題/答案的http://計算器。 com/questions/3062600/use-jquery-cookies-in-showing-hiding-elements-jquery?rq = 1 – Reina 2013-04-10 03:21:49

+0

手頭的問題不是cookies,因爲我不會在javas嚴厲打擊這項工作。現在的問題是如何創建多個數組,而無需動態地手動分配和讀取特定的Cookie。 – EasyBB 2013-04-10 11:48:51

回答

0

你試過了jQuery Cookie插件嗎?

https://github.com/carhartl/jquery-cookie

然後創建到期的cookie,橫跨整個站點有效:

$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' }); 

然後讀取cookie:

$.cookie('the_cookie'); // => "the_value"