简体   繁体   English

jquery上的手风琴。 我无法关闭活动元素

[英]accordion on jquery. I can not close active element

I'm trying to create an accordion effect. 我正在尝试创建手风琴效果。 I can do so that the section is expanded but I can not figure out how to close one already open. 我可以这样做,以扩大该部分,但我不知道如何关闭一个已经打开的部分。

Can you give me a hand? 你能帮我个忙吗?

function cws_accordion_init (){
    jQuery.fn.cws_accordion = function () {
        jQuery(this).each(function (){
            var sections = jQuery(this).find(".accordion_section");
            sections.each( function (index, value){
                var section_index = index;
                jQuery(this).find(".accordion_title").on("click", function (){
                    jQuery(this).siblings(".accordion_content").slideDown("300");
                    sections.eq(section_index).addClass("active");
                    sections.eq(section_index).siblings().removeClass("active").find(".accordion_content").slideUp("300");
                });
            });
        });
    }
}

html html

<section class='cws-widget'>

    <section class='cws_widget_content toggle_widget'>
        <div class='accordion_section featured_check_up'>
            <div class='accordion_title'>title 1</div>
            <div class='accordion_content' style='display: none;'>
                <p>fhshjsjf isfi fhsuhsj dsihdisj dshd disd jdijd shd is disu</p>
            </div>
        </div>

        <div class='accordion_section featured_check_up'>
            <div class='accordion_title'>title 2</div>
            <div class='accordion_content' style='display: none;'>
                <p>djjdjdijisjdisjidjsijdisjdisjdis</p>
            </div>
        </div>



    </section>

</section>

This will toggle the accordion that is selected and close all the others: 这将切换选定的手风琴并关闭所有其他手风琴:

$(".accordion_title").on("click", function (){
  $(this).siblings(".accordion_content").slideToggle("300");
  $(this).addClass("active");
  $(".accordion_title").not(this).removeClass("active");
  $(".accordion_title").not(this).siblings(".accordion_content").slideUp("300");
});

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

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