手风琴修复(jquery)
HTML
<h2 class="sec-title">one</h2>
<div class="sec-content">content 1</div>
<h2 class="sec-title">two</h2>
<div class="sec-content">content 2</div>
jQuery:
$('.sec-content').hide();
$('.sec-title:first').addClass('active').next().show();
$('.sec-title').click(function(){
if( $(this).next().is(':hidden') ) {
$('.sec-title').removeClass('active').next().slideUp();
$(this).toggleClass('active').next().slideDown();
}
return false;
});
... 有效,但是如果想让第二个手风琴部分在页面加载时处于活动状态怎么办?我要改变什么?
感谢您的帮助
HTML
<h2 class="sec-title">one</h2>
<div class="sec-content">content 1</div>
<h2 class="sec-title">two</h2>
<div class="sec-content">content 2</div>
jQuery:
$('.sec-content').hide();
$('.sec-title:first').addClass('active').next().show();
$('.sec-title').click(function(){
if( $(this).next().is(':hidden') ) {
$('.sec-title').removeClass('active').next().slideUp();
$(this).toggleClass('active').next().slideDown();
}
return false;
});
... works but what if want to make the second accordion section active on pageload? what do I change?
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将选择第一个
sec-title
类元素的行更改为:这是
:eq
选择器的 API 文档。我想它会做你想做的。它将选择与选择器匹配的第 n 个元素。请记住,eq()
是从 0 开始的 - 因此:eq(1)
选择的是具有sec-title
类的第二个项目。这是我的小提琴,说明了这一点: http://jsfiddle.net/7KdGn/1/
我希望这有帮助!
Try changing the line where you select the first
sec-title
class element to this:Here's the API documentation for the
:eq
selector. I think it will do what you want. It will select the nth element that matches the selector. Remember thateq()
is 0-based - so:eq(1)
is selecting the second item that has thesec-title
class.Here's my fiddle that illustrates this: http://jsfiddle.net/7KdGn/1/
I hope this helps!
您可以使用 (jQuery UI Accordion](http://docs.jquery.com/UI/Accordion) 而不是重新发明轮子,请密切关注其 active 选项。然后您的代码将变得简单到
此处查看示例。
You may use (jQuery UI Accordion](http://docs.jquery.com/UI/Accordion) instead of reinventing the wheel. Pay close attention to its active option. Then your code will become simplified to as simple as
Take a look for an example here.