【问题标题】:Keeping A jQuery menu open on refresh or on click with cookies在刷新或单击时使用 cookie 保持 jQuery 菜单打开
【发布时间】:2017-05-23 04:36:23
【问题描述】:

我正在尝试使https://ionianbookstore.com/ 的下拉菜单保持活动状态单击子菜单或刷新,但它不起作用。

https://stackoverflow.com/questions/20396116/keeping-a-jquery-menu-open-with-cookies#= 这个帮助了我,将代码更改为我的插件代码但仍然无法正常工作,我知道我没有让它正确,所以这就是为什么我打开一个新问题以寻求帮助:)

插件的.js就是这个:

!function(e){function n(e){e.hasClass("toggle__open")?e.removeClass("toggle__open"):e.addClass("toggle__open"),e.parent(".link__wrap").parent(".menu-item").children(".sub-menu").slideToggle("fast")}e(document).on("click",".easy-sidebar-menu-widget-toggler",function(t){n(e(this)),t.preventDefault(),t.stopPropagation()})}(jQuery);

这是另一个问题的解决方案(这个我曾经让它工作但我没有做到) http://jsfiddle.net/pHgB7/2/

感谢您的阅读帮助:)

【问题讨论】:

    标签: javascript jquery cookies


    【解决方案1】:

    尝试停止在您的侧边菜单被点击的子菜单上的传播。

      $("#yoursidemenu li a")
        .on('click', function (e) {
            e.stopPropagation();
        });
    

    【讨论】:

      【解决方案2】:

      据我所知,您所要做的就是在您想要切换的easy-sidebar-menu-widget-toggler 元素上触发以下操作。

      e.addClass("toggle__open"), e.parent(".link__wrap").parent(".menu-item").children(".sub-menu").slideToggle("fast")
      

      因此您可以在元素上添加一个属性,然后设置一个 cookie 以了解您希望在每次点击刷新后打开哪个

      <a href="#" class="easy-sidebar-menu-widget-toggler" data-toggle="history"><li>...</li></a>
      
      $(function(){
          $('.easy-sidebar-menu-widget-toggler').on('click',function(){
              //Setup the cookie on click here
              $.cookies('myToggle',$(this).data('history'));
          });
      
          //then on the init of the page you can do
          //a check and if the cookie exists then toggle fine the 
          //a href and then toggle it.
          if($.cookies('myToggle'))
          {
              var toToogle = $.cookies('myToggle');
              var aToToggle = $('body').find('a[data-toggle="'+ toToggle + '"');
              aToToggle.addClass('toggle__open');
              aToToggle.parent(".link__wrap").parent(".menu-item").children(".sub-menu").slideToggle("fast");
          }
      });
      

      别忘了,如果用户在没有切换器的情况下单击菜单,您需要实现一些逻辑来删除 cookie。

      【讨论】:

        猜你喜欢
        • 2013-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多