简体   繁体   English

手风琴打开但不会关闭

[英]Accordion Opens But Won't Close

I am using Foundation Accordion and I added a script to have smooth opening and closing. 我正在使用Foundation Accordion,并且添加了一个脚本,可以顺利地打开和关闭。 It works but the problem is if you have only one panel you can open it but not close it. 它可以工作,但问题是,如果只有一个面板,则可以打开它,但不能关闭它。

Here is the code used: 这是使用的代码:

$(".accordion").on("click", "dd:not(.active)", function (event) {
    $("dd.active").removeClass('active').find(".content").slideUp("slow");
    $(this).addClass('active').find(".content").slideToggle("slow");
}); 

I tried this code but it didn't work: 我尝试了这段代码,但是没有用:

$("dd.active").on("click", function (event) {
    $("dd.active").removeClass('active').find(".content").slideUp("slow");
});

How do I get this to close smoothly? 如何使它顺利关闭?

Here is a jsFiddle 这是一个jsFiddle

Note: Open the Panel then Try to Close It. 注意:打开面板,然后尝试关闭它。

Remove href from <dd><a> . <dd><a>删除href

JS: JS:

$(function () {
    $(".accordion").on("click", "dd", function (event) {
        if (!($(this).hasClass("active"))) {
            $("dd.active").removeClass('active').find(".content").slideUp("fast");            
        }
        $(this).toggleClass('active').find(".content").slideToggle("fast");
    })
});

Updated fiddle here. 在这里更新了小提琴。

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

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