jquery 无需插件即可切换手风琴
您好,有两个内容部分,我只想一次打开 1 个。它们都通过单击标题来折叠和打开。我目前的工作方式是它们彼此独立,但我希望它们在显示时隐藏对方。我不想使用 jquery 手风琴插件。
这是我现在拥有的代码:
HTML:
<div class="topCats">
<h4><a href="#" class="openBox">Most Searched Categories</a></h4>
</div>
<div id="topCatsContainer">
content 1
</div>
<div class="allCats">
<h4><a href="#" class="closed">All Categories</a></h4>
</div>
<div id="allCatsContainer">
content 2
</div>
JS:
$('#allCatsContainer').css("display", "none");
$('.topCats h4 a').click( function() {
$('#topCatsContainer').slideToggle(500);
$(this).toggleClass("openBox");
});
$('.allCats h4 a').click( function() {
$('#allCatsContainer').slideToggle(500);
$(this).toggleClass("openBox");
});
任何帮助将不胜感激。
谢谢
Hi have two content sections and i only want 1 open at a time. They both collapse and open based on clicking the headings. I have it currently working where they are independent of each other but i am looking to have them hide the other when it is shown. I dont want to have to use the plugin for jquery accordion.
here is the code i have now:
HTML:
<div class="topCats">
<h4><a href="#" class="openBox">Most Searched Categories</a></h4>
</div>
<div id="topCatsContainer">
content 1
</div>
<div class="allCats">
<h4><a href="#" class="closed">All Categories</a></h4>
</div>
<div id="allCatsContainer">
content 2
</div>
JS:
$('#allCatsContainer').css("display", "none");
$('.topCats h4 a').click( function() {
$('#topCatsContainer').slideToggle(500);
$(this).toggleClass("openBox");
});
$('.allCats h4 a').click( function() {
$('#allCatsContainer').slideToggle(500);
$(this).toggleClass("openBox");
});
Any help would be greatly appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最近在这里回答了这个问题:当另一个 div 打开时,我需要什么代码来折叠另一个 div?
最简单的方法是折叠两个 div,然后展开您想要的那个。已经折叠的将被忽略。
更新:以上对他不起作用,因为
和
之间没有直接链接。这真的很难看,但以下作品 http://jsfiddle.net/mrtsherman/MPwQ8/3/
有很多更好的方法可以做到这一点。我为您提供的初始链接有一个示例。如果你想要更强大的东西,那么你需要创建一些方法来逻辑地将你的链接绑定到你的 div。
This was answered recently here: What code do i need to collapse a div when another is open?
The easy way is to collapse both, then expand the one you want. The one that is already collapsed will be ignored.
Update: Above won't work for him because there is no direct link between
<a>
and<div>
. This is really ugly, but the following works http://jsfiddle.net/mrtsherman/MPwQ8/3/There are lots better ways to do this. The initial link I provided you has an example. If you want something more robust then you need to create some means to logically tie your links to your divs.
如果可能的话,我尽量不改变你的 HTML,这样你就不必处理令人头痛的样式问题。不幸的是,这意味着 JS 的编码比我想要的要硬一些。
HTML:
HTML 是相同的,只是我向每个内容 div 添加了一个类。
JS:
基本上,JS 会查找与单击的链接相匹配的内容。如果内容可见,那么它就会自行关闭。如果它不可见,那么我们关闭可见的内容并打开单击的内容。
如果您注意到我正在切换
openBox
和close
类,这可能是多余的,但我不确定您的样式是否依赖它。一般来说,这个功能可以通过多种方式来完成,如果您愿意稍微改变一下 HTML,我们可以让 JS 端看起来更漂亮一点:) 否则您可以只使用这个实现并避免任何样式问题。
I tried to not alter your HTML if possible, so that you wouldn't have to deal with styling headaches. Unfortunately this means the JS is a bit more hard coded than I would like.
HTML:
The HTML is the same except I added a class to each of your content div.
JS:
Basically the JS finds the content that matches the link that was clicked. If the content was visible then it just closes itself. If it wasn't visible then we close whichever was visible and open the clicked content.
If you notice I am toggling both the
openBox
andclosed
class, which can be redundant but I wasn't sure if your styling relied on it or not.In general this functionality can be accomplished many ways and if you are willing to change your HTML a bit we could have the JS side look a bit more presentable :) Otherwise you can just use this implementation and avoid any styling headaches.