jQuery 手风琴和 cookie 的问题

发布于 2024-08-31 09:20:07 字数 2206 浏览 2 评论 0原文

我有一个来自 this 网站的 jQuery 手风琴,并为我的目的,但手风琴仅适用于 Firefox(不适用于 Safari 或 Chrome)并且 cookie 设置不正确。

这是 jQuery:

            function initMenus() {
                $('#sidebar .letter_index').hide();
                $.each($('#sidebar .letter_index'), function() {
                    var cookie = $.cookie(this.id);
                    if (cookie === null || String(cookie).length < 1) {
                        $('#sidebar .letter_index:first').show();
                    }   else {
                        $('#' + this.id + ' .' + cookie).next().show();
                    }
                });
                $('#sidebar .letter_head').click(function() {
                    var checkElement = $(this).next();
                    var parent = this.parentNode.parentNode.id;

                    if ((checkElement.is('.letter_index')) && (!checkElement.is(':visible'))) {
                        $('#' + parent + ' .letter_index:visible').slideUp('normal');
                            if ((String(parent).length > 0) && (String(this.className).length > 0)) {
                                // not working
                                $.cookie(parent, this.className);
                            }
                        checkElement.slideDown('normal');
                        return false;
                    }
                }
                );
            }
            $(document).ready(function() {initMenus();});

这是我的 HTML 的外观(示例项):

            <div id="sidebar">
      <h2 class="letter_head"><a href="#" class="ABC">ABC</h2>
      <ul class="letter_index">
        <li>Abc</li>
        <li>Bcd</li>
      </ul>
            </div>

我找不到脚本无法在 Safari 和 Chrome 中工作的问题。

我也不知道如何告诉它使用 h2 内的 a 类作为 cookie 值。 (当前 cookie 设置为 $.cookie(parent, this.className);,它会生成名称为 container (#sidebar 上面的 div)和值的 cookie DEF”等。

letter_head。它需要类似于“sidebar”和“ABC”、 “

I have a jQuery accordion from this site and edited for my purposes, but the accordion only works in Firefox (not Safari or Chrome) and the cookies aren't being set correctly.

This is the jQuery:

            function initMenus() {
                $('#sidebar .letter_index').hide();
                $.each($('#sidebar .letter_index'), function() {
                    var cookie = $.cookie(this.id);
                    if (cookie === null || String(cookie).length < 1) {
                        $('#sidebar .letter_index:first').show();
                    }   else {
                        $('#' + this.id + ' .' + cookie).next().show();
                    }
                });
                $('#sidebar .letter_head').click(function() {
                    var checkElement = $(this).next();
                    var parent = this.parentNode.parentNode.id;

                    if ((checkElement.is('.letter_index')) && (!checkElement.is(':visible'))) {
                        $('#' + parent + ' .letter_index:visible').slideUp('normal');
                            if ((String(parent).length > 0) && (String(this.className).length > 0)) {
                                // not working
                                $.cookie(parent, this.className);
                            }
                        checkElement.slideDown('normal');
                        return false;
                    }
                }
                );
            }
            $(document).ready(function() {initMenus();});

This is how my HTML looks (sample item):

            <div id="sidebar">
      <h2 class="letter_head"><a href="#" class="ABC">ABC</h2>
      <ul class="letter_index">
        <li>Abc</li>
        <li>Bcd</li>
      </ul>
            </div>

I can't find the problem why the script won't work in Safari and Chrome.

I also don't know how to tell it to use the class of the a inside the h2 as the cookie value. (Currently the cookie is set as $.cookie(parent, this.className);, which produces cookies with the name container (the div above #sidebar) and the value letter_head. It needs to be something like 'sidebar' and 'ABC', 'DEF' and so on.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2024-09-07 09:20:08

我为您发布了演示。我必须重写一堆代码,因为原始脚本是为与多个手风琴一起使用而编写的,但我假设您只想使用这个脚本。无论如何,这是我使用的 HTML:

<div id="container">  
 <div id="sidebar">

  <h2 class="letter_head"><a href="#" class="ABC1">ABC1</a></h2>
  <ul class="letter_index">
   <li>Abc1</li>
   <li>Bcd1</li>
  </ul>

  <h2 class="letter_head"><a href="#" class="ABC2">ABC2</a></h2>
  <ul class="letter_index">
   <li>Abc2</li>
   <li>Bcd2</li>
  </ul>

  <h2 class="letter_head"><a href="#" class="ABC3">ABC3</a></h2>
  <ul class="letter_index">
   <li>Abc3</li>
   <li>Bcd3</li>
  </ul>

 </div>  
</div>

脚本:

function initMenus() {
 var sidebar = $('#sidebar');
 sidebar.find('ul.letter_index').hide();
 var cookie = $.cookie( sidebar.attr('id') );
 if (cookie === null || String(cookie).length < 1) {
  sidebar.find('.letter_index:first').show();
 } else {
  sidebar.find(('h2 > a.' + cookie)).parent().next().show();
 }

 sidebar.find('h2.letter_head').click(function(){
  var checkElement = $(this).next();
  if ((checkElement.is('.letter_index')) && (!checkElement.is(':visible'))) {
   sidebar.find('.letter_index:visible').slideUp('normal');
   var headClassName = $(this).find('a').attr('class').trim();
   if (headClassName.length > 0) {
    $.cookie( sidebar.attr('id'), headClassName );
   }
   checkElement.slideDown('normal');
   return false;
  }
 });
}
$(document).ready(function() {initMenus();});

I posted a demo for you. I had to rewrite a bunch of the code because the original script was written to work with multiple accordions, but I am going to assume you only want to use this script for one. Anyway, here is the HTML I used:

<div id="container">  
 <div id="sidebar">

  <h2 class="letter_head"><a href="#" class="ABC1">ABC1</a></h2>
  <ul class="letter_index">
   <li>Abc1</li>
   <li>Bcd1</li>
  </ul>

  <h2 class="letter_head"><a href="#" class="ABC2">ABC2</a></h2>
  <ul class="letter_index">
   <li>Abc2</li>
   <li>Bcd2</li>
  </ul>

  <h2 class="letter_head"><a href="#" class="ABC3">ABC3</a></h2>
  <ul class="letter_index">
   <li>Abc3</li>
   <li>Bcd3</li>
  </ul>

 </div>  
</div>

Script:

function initMenus() {
 var sidebar = $('#sidebar');
 sidebar.find('ul.letter_index').hide();
 var cookie = $.cookie( sidebar.attr('id') );
 if (cookie === null || String(cookie).length < 1) {
  sidebar.find('.letter_index:first').show();
 } else {
  sidebar.find(('h2 > a.' + cookie)).parent().next().show();
 }

 sidebar.find('h2.letter_head').click(function(){
  var checkElement = $(this).next();
  if ((checkElement.is('.letter_index')) && (!checkElement.is(':visible'))) {
   sidebar.find('.letter_index:visible').slideUp('normal');
   var headClassName = $(this).find('a').attr('class').trim();
   if (headClassName.length > 0) {
    $.cookie( sidebar.attr('id'), headClassName );
   }
   checkElement.slideDown('normal');
   return false;
  }
 });
}
$(document).ready(function() {initMenus();});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文