【问题标题】:jQuery cookie not working properly in wordpressjQuery cookie 在 wordpress 中无法正常工作
【发布时间】:2015-06-22 21:49:29
【问题描述】:

我用html + jquery 制作了我自己的手风琴菜单,它可以完美地工作到第四级,然后我从网络添加一个 cookie 代码并做一些额外的工作,它在 html 中也可以 100% 完美地工作。

我完整的 HTML + jQuery 代码参考在 JS-Fiddle Accordion Menu with Cookie

现在我非常小心地将它合并到我的 wordpress 菜单中,它表现出非常奇怪的行为。当我单击加子菜单打开并单击子类别时,页面转到该类别但菜单已关闭,但是当我再次打开子菜单并单击菜单或刷新页面时,它的工作。我担心为什么在 wordpress 中 jquery cookie 不起作用。

这里是 jquery cookie + jquery 手风琴代码:

jQuery(document).ready(function () {


   // baking cookie ;p
   function set_cookie(ID) {
         document.cookie = ID + "=opened";
   }

   // getting it out from the oven... ;)
   function get_cookies_array() {

       var cookies = {};

       if (document.cookie && document.cookie != '') {
          var split = document.cookie.split(';');
          for (var i = 0; i < split.length; i++) {
              var name_value = split[i].split("=");
              name_value[0] = name_value[0].replace(/^ /, '');
              cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
          }
       }

       return cookies;

   }

   // yuck... sorry i don't know how to cook :S
   function unset_cookie(cookie_name) {
        var cookie_date = new Date();
        cookie_date.setTime(cookie_date.getTime() - 1);
        document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
   }

   var tree_id = 0;
   jQuery('ul.b29-tree li:has(ul)').addClass('has-child').prepend('<span class="switch">+</span>').each(function () {
                    tree_id++;
                    jQuery(this).attr('id', 'tree' + tree_id);
                });
   // Accordion code
   jQuery('ul.b29-tree li > span.switch').click(function () {
            var tree_id = jQuery(this).parent().attr('id');
            if (jQuery(this).hasClass('open')) {
                        jQuery(this).parent().find('ul:first').slideUp('fast');
                        jQuery(this).removeClass('open');
                        jQuery(this).text('+');
                        unset_cookie(tree_id)
                    } else {
                        jQuery(this).parent().find('ul:first').slideDown('fast');
                        jQuery(this).text('-');
                        jQuery(this).addClass('open');
                        set_cookie(tree_id)

                    }
                });

                var cookies = get_cookies_array();
                for (var name in cookies) {
                    $('#' + name).find('> ul').css({'display' : 'block'});
                    $('#' + name).find('> span').addClass('open').text('-');
                }

            });

我在我的 xamp 上使用 wordpress,所以我不能给你那个链接,但上面的链接是 html 的演示

【问题讨论】:

    标签: jquery html wordpress cookies


    【解决方案1】:

    好的,

    我自己解决了这个问题。我使用 wordpress cookiepath 常量,它可以在 wordpress 上完美运行。

    这里是jquery代码:

    jQuery(document).ready(function () {
                    // baking cookie ;p
                    function set_cookie(ID) {
                        document.cookie = ID + "=opened; path=<?php echo COOKIEPATH ?>";
                    }
    
                    // getting it out from the oven... 
                    function get_cookies_array() {
                        var cookies = {};
    
                        if (document.cookie && document.cookie != '') {
                            var split = document.cookie.split(';');
                            for (var i = 0; i < split.length; i++) {
                                var name_value = split[i].split("=");
                                name_value[0] = name_value[0].replace(/^ /, '');
                                cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
                            }
                        }
                        return cookies;
                    }
    
                    // yuck... sorry i don't know how to cook :S
                    function unset_cookie(cookie_name) {
                        var cookie_date = new Date();
                        cookie_date.setTime(cookie_date.getTime() - 1);
                        document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString() + "; path=<?php echo COOKIEPATH ?>";
                    }
    
                    var tree_id = 0;
                    jQuery('ul.wpc-categories li:has(ul)').addClass('has-child').prepend('<span class="switch"><img src="<?php echo plugin_dir_url(__FILE__); ?>/includes/css/images/icon-plus.png" /></span>').each(function () {
                        tree_id++;
                        jQuery(this).attr('id', 'tree' + tree_id);
                    });
    
                    jQuery('ul.wpc-categories li > span.switch').click(function () {
                        var tree_id = jQuery(this).parent().attr('id');
                        if (jQuery(this).hasClass('open')) {
                            jQuery(this).parent().find('ul:first').slideUp('fast');
                            jQuery(this).removeClass('open');
                            jQuery(this).html('<img src="<?php echo plugin_dir_url(__FILE__); ?>/includes/css/images/icon-plus.png" />');
                            unset_cookie(tree_id)
                        } else {
                            jQuery(this).parent().find('ul:first').slideDown('fast');
                            jQuery(this).html('<img src="<?php echo plugin_dir_url(__FILE__); ?>/includes/css/images/icon-minus.png" />');
                            jQuery(this).addClass('open');
                            set_cookie(tree_id)
                        }
                    });
    
                    var cookies = get_cookies_array();
                    for (var name in cookies) {
                        jQuery('#' + name).find('> ul').css({'display' : 'block'});
                        jQuery('#' + name).find('> span').addClass('open').html('<img src="<?php echo plugin_dir_url(__FILE__); ?>/includes/css/images/icon-minus.png" />');
                    }
                });
    

    【讨论】:

      猜你喜欢
      • 2011-07-28
      • 1970-01-01
      • 2020-07-02
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      相关资源
      最近更新 更多