jQuery 手风琴和 cookie 的问题
我有一个来自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我为您发布了演示。我必须重写一堆代码,因为原始脚本是为与多个手风琴一起使用而编写的,但我假设您只想使用这个脚本。无论如何,这是我使用的 HTML:
脚本:
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:
Script: