简体   繁体   English

当其中一个面板按手风琴打开时,关闭其他面板

[英]Close other panels when one of the panel opened in accordion

How to close all panel in the accordion when one of the panel opened? 当其中一个面板打开时,如何关闭手风琴中的所有面板? My panel Accordion is still open. 我的小组手风琴仍在开放。 The expectation is when i click the one of panel, other panel is automatically closed. 期望是当我点击其中一个面板时,其他面板会自动关闭。

This is my HTML, CSS and JS 这是我的HTML,CSS和JS

 $(".acitemx h3").click(function() { $header = $(this); $content = $header.next(); $content.slideToggle(500, function() { $(this).parent().toggleClass('current'); }); }); $('.acitemx').eq(0).addClass('.acitemx').find('.accx').css('display', 'block'); 
 /* accordion editing */ .accx { background: #FFF none repeat scroll 0% 0%; padding: 15px; display: none; } .acitemx { margin-bottom: 10px; box-shadow: 0px 0px 2px 0px rgb(189, 189, 189); } .acitemx h3 { cursor: pointer; background: #00ADEF none repeat scroll 0% 0% !important; color: #FFF !important; font-family: "roboto", sans-serif; font-weight: bold; padding: 8px 40px 8px 15px !important; position: relative; border-radius: 3px 3px 3px 3px; } /* end accordion editing */ 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="accordionx"> <div class="acitemx"> <h3>First Panel</h3> <div class="accx"> This is the content </div> </div> <div class="acitemx"> <h3>Second Panel</h3> <div class="accx"> This is the content </div> </div> <div class="acitemx"> <h3>Third Panel</h3> <div class="accx"> This is the content </div> </div> </div> 

This the JSFiddle link http://jsfiddle.net/bupd32rq/3/ 这个JSFiddle链接http://jsfiddle.net/bupd32rq/3/

Thanks for your help :) 谢谢你的帮助 :)

You can use the slideUp() function after finding other siblings accx element. 您可以在找到其他兄弟accx元素后使用slideUp()函数。

$(this).parent().siblings().find('.accx').slideUp();

Not related to answer: $('.acitemx').eq(0).addClass('.acitemx').find('.accx').css('display', 'block'); 与答案无关: $('.acitemx').eq(0).addClass('.acitemx').find('.accx').css('display', 'block'); Here addClass need not have . 这里addClass不需要. and there is no logic adding another class of same name as they already have the CSS properties set. 并且没有逻辑添加另一个同名的类,因为他们已经设置了CSS属性。

JSfiddle demo JSfiddle演示

Code snippet 代码段

 $(".acitemx h3").click(function() { $header = $(this); $content = $header.next(); $content.slideToggle(500, function() { $(this).parent().toggleClass('current'); }); $(this).parent().siblings().find('.accx').slideUp(); // Added code }); $('.acitemx').eq(0).find('.accx').css('display', 'block'); 
 /* accordion editing */ .accx { background: #FFF none repeat scroll 0% 0%; padding: 15px; display: none; } .acitemx { margin-bottom: 10px; box-shadow: 0px 0px 2px 0px rgb(189, 189, 189); } .acitemx h3 { cursor: pointer; background: #00ADEF none repeat scroll 0% 0% !important; color: #FFF !important; font-family: "roboto", sans-serif; font-weight: bold; padding: 8px 40px 8px 15px !important; position: relative; border-radius: 3px 3px 3px 3px; } /* end accordion editing */ 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="accordionx"> <div class="acitemx"> <h3>First Panel</h3> <div class="accx"> This is the content </div> </div> <div class="acitemx"> <h3>Second Panel</h3> <div class="accx"> This is the content </div> </div> <div class="acitemx"> <h3>Third Panel</h3> <div class="accx"> This is the content </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM