jQuery 1.3.2 中的手风琴无法工作(在 1.3 中可以工作!)
好吧,所以某个地方出了问题。当我使用 1.3 版本时,手风琴工作正常。当我将 jQuery 升级到版本 1.3.2 时,它现在不再按预期工作。我需要最新版本,因为它解决了 IE6 抛出的一些错误...
所以这是代码,需要更改什么才能使其与 jQuery 的最新版本一起工作?
$(function() {
$(".accordion h4").eq(2).addClass("active");
$(".accordion div").eq(2).show();
$(".accordion h4").click(function(){
$(this).next(".accordion div").slideToggle("slow")
.siblings("div:visible").slideUp("slow");
$(this).toggleClass("active");
$(this).siblings("h4").removeClass("active");
});
$("div.accordion div").hide();
$("h4#open").trigger('click');
});
HTML 是这样的:
<div class="accordion">
<h4 id="open">Content header</h4>
<div>
<ul>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
</ul>
</div>
<h4>Content header</h4>
<div>
<ul>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
</ul>
</div>
<h4>Content header</h4>
<div>
<ul>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
</ul>
</div>
我应该注意到,它不起作用的地方是,当展开第二个面板时,第一个面板应该正常折叠 - 但在 1.3.2 中,情况并非如此......
*我已经用我真正的内容更新了 HTML一直在发生,因为看起来是 HTML/CSS 出了问题。
我将CSS设置为:
li strong{display:block;float:left;width:250px;background:#ccc;}
li em{display:block;float:left;width:700px;background:#ddd;}
为了让它给出“类似2列的表格”效果(并且为了方便客户端CMS编辑 - 点击“粗体”按钮并点击“i”按钮) - 但看起来正是这种元素的浮动导致手风琴无法工作。当我删除“float: left;”时- 它再次正常工作。
并且它并没有消除这样一个事实,即相同的设置适用于 jquery1.3.0 但不适用于 jquery1.3.2 - 所以仍然有问题!
Ok, so somewhere, something is wrong. When I was using version 1.3 the accordion was working fine. When I upgraded jQuery to version 1.3.2 it now no longer works as it's supposed to. I need the latest version because it is solving some errors that IE6 was throwing up...
So here's the code, and what needs to change in order for it to work with jQuery's latest vesion?
$(function() {
$(".accordion h4").eq(2).addClass("active");
$(".accordion div").eq(2).show();
$(".accordion h4").click(function(){
$(this).next(".accordion div").slideToggle("slow")
.siblings("div:visible").slideUp("slow");
$(this).toggleClass("active");
$(this).siblings("h4").removeClass("active");
});
$("div.accordion div").hide();
$("h4#open").trigger('click');
});
HTML goes something like:
<div class="accordion">
<h4 id="open">Content header</h4>
<div>
<ul>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
</ul>
</div>
<h4>Content header</h4>
<div>
<ul>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
</ul>
</div>
<h4>Content header</h4>
<div>
<ul>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
<li><strong>Something</strong> <em>Else</em></li>
</ul>
</div>
I should note, that where it is not working, is that when expanding a second panel, the first panel should collapse as normal - but with 1.3.2 it is not the case...
*I have updated the HTML with what I really had going on, as it seems that it was the HTML/CSS that was giving problems.
I had the CSS set to:
li strong{display:block;float:left;width:250px;background:#ccc;}
li em{display:block;float:left;width:700px;background:#ddd;}
in order for it to give a "2-columned table-like" effect (and for easy client-CMS editing - hit the "bold" button and hit the "i" button) - but it seems that it was this floating of the elements that made the accordion not work. When I removed the "float: left;" - it worked again as normal.
And it doesn't take away from the fact that this same set-up works with jquery1.3.0 but not with jquery1.3.2 - so something is still amiss!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您的选择器
$(this).next(".accordion div")
将不起作用。 next() 只获取下一个同级,因此执行一个查看子级的过滤器很奇怪。还尝试使用带有 nodeName 的选择器而不是纯 css 类选择器。
在你的js中使用
div.accordian
而不仅仅是.accordian
尝试
I believe your selector
$(this).next(".accordion div")
will not work. next() only grabs the next sibling therefore doing a filter that looks at children is odd.Also try to use selectors with a nodeName rather than a pure css class selector.
Use
div.accordian
rather than just.accordian
in your jsTry
您可能还需要升级您的手风琴库。某些样式选择器(例如 Accordion 使用的
[@attr]
样式选择器)可能已被弃用。You might need to upgrade your Accordion library as well. Some style selectors like the
[@attr]
style selectors that the Accordion uses might have been deprecated already.