0

我有一个动态生成的页面,slideToggle用于打开和关闭分层divs 等没问题。唯一的问题是,每次我回发时,我都必须div再次生成 s 并且它们失去了打开/关闭状态。它们总是使用相同的唯一ids 生成。

我想使用 cookie 插件来记住调用sltoggle函数时的状态,然后在页面重新加载时扩展所有相同的 div。这是我到目前为止所得到的......

    $(document).ready(function () 
    {
        $(".toggle-hide").hide();
        //something in here about opening the divs in the cookie
    });

    function sltoggle(eID) 
    {
        $("div[id$='" + eID + "']").slideToggle(600);
        //I think the below code is okay - I copied it from a working example ^^
        var divState = ($("div[id$='" + eID + "']").css('display') == 'block') ? 1 : 0;
        $.cookie("divState", state)
     } 
4

1 回答 1

1

内联评论解释。

function slToggle(eID) {
   var $div = $("div[id$='" + eDI + "']");
   //Get value of cookie or empty string
   //Cookie is list of eIDs that should be visible
   var cooks = $.cookie("divState") || '';

   //Determine whether eID is already in the cookie
   var isin = $.inArray(eID, cooks.split(','));

   //TODO verify that .is("visible") check works during
   //toggle animation.  Otherwise, this code goes in the
   //toggle animation callback function
   if ($div.slideToggle(600).is(":visible")) {
      //Div is visible, but not in cookie
      if (!isin) {
         $.cookie("divState", cooks + (cooks ? ',' : '') + eID);
      }
   }
   else if (isin) {
      //Div not visible, but in cookie
      $.cookie("divState", cooks.replace(/(^|,)eID(,|$)/, ''));
   }
}
于 2012-10-02T09:03:58.883 回答