0
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设置为使其向上滑动并保持隐藏有效,但是当我尝试打开它时,它会向下滑动,然后我刷新页面,它不会像应有的那样保持打开状态。

为了得到图片 - http://jsfiddle.net/zRT9u/

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

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

新编辑-尝试了该$.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);
   }
4

1 回答 1

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函数以及添加"#"+到 map 函数,我能够获得实际的 ID 名称。然后我开始设置newCookiec.join(',') 之后一切正常!

于 2013-06-16T04:09:49.870 回答