Cookie代码不会删除cookie的已打开元素

时间:2013-06-16 02:45:33

标签: javascript jquery cookies

var openClose = $('.openClose');
openClose.on('click', function() {
   var cook = ReadCookie('slideHide'),
       miniParent = $(this).parent().parent().parent().children('.main-content'),
       miniDisp = miniParent.css('display');
     if (miniDisp ==="block") {
         KillCookie('slideHide');
   $(this).parent().parent().parent().children('.main-content').slideUp();
   var slide = cook + "," + "#"+$(this)
                         .parent()
                         .parent()
                         .parent()
                         .parent().attr("id") +
                         " #"+$(this).parent()
                         .parent().parent().attr("id");
            SetCookie('slideHide', slide, 100);
        } 
        else
        {
   $(this).parent().parent().parent().children('.main-content').slideDown();
       var newCookie=[];
  var a= $('.module').children('.main-content').filter(":hidden");
       for(var i=0;i<a.length;i++){
         var d = $(a[i++]);
        var c = "#"+d.parent('.module').attr('id');
       }
       newCookie= c;
       console.log(newCookie);
               KillCookie('slideHide');
               SetCookie('slideHide',d, 100);
        }
  });

这些是我的cookie功能:

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

设置cookie以使其滑动并保持隐藏状态,但是当我尝试打开它时,它会slideDown,然后我刷新页面它不会保持打开状态。

要获得图片 - http://jsfiddle.net/zRT9u/

如果你需要了解更多,请问我,我愿意提供更多!

我编辑了javascript它几乎可以工作,但我没有得到我需要的所有对象

NEW EDIT-尝试了$.map()功能,但当我打开一个,并刷新所有这些功能现在都已打开?

  else {
    $(this).parent().parent().parent().children('.main-content').slideDown();
KillCookie('slideHide');
    var newCookie=[];
    var a= $('.module').children('.main-content').filter(":hidden");
    var c = $.map(a,function(n,i){
      return $(n).parent().attr('id');
    });
    newCookie= c;
    SetCookie('slideHide',newCookie, 100);
   }

1 个答案:

答案 0 :(得分:0)

使用$.map.join()

修复了此问题
var openClose = $('.openClose');
  openClose.on('click', function() {
  var cook = ReadCookie('slideHide'),
      miniParent = $(this).parent().parent().parent().children('.main-content'),
      miniDisp = miniParent.css('display');
         if (miniDisp ==="block") {
            KillCookie('slideHide');
$(this).parent().parent().parent().children('.main-content').slideUp();
       var slide = cook+","+ "#"+$(this).parent().parent().parent().attr("id");
         SetCookie('slideHide', slide, 100);
        } else {
$(this).parent().parent().parent().children('.main-content').slideDown();
     KillCookie('slideHide');
    var newCookie=[],
    a= $('.module').children('.main-content').filter(":hidden"),
    c = $.map(a,function(n,i){
    return "#"+$(n).parent().attr('id');
 });
 newCookie= c.join(',');
  SetCookie('slideHide',newCookie, 100);
  }
});

通过创建“全局”数组然后使用$.map函数以及向地图函数添加"#"+,我能够获得实际的ID名称。然后我将newCookie设置为c.join(','),之后一切正常!