简体   繁体   English

再次单击头部时关闭手风琴

[英]Close of the accordion when click once again in the head

I'm using jQuery to althern the open and close of a some accordions from Django Data, the data shows correctly, when I click another element the rest of accordions close, its ok, but when I click again in the same head of this accordion, this one not close just return open once again.我正在使用 jQuery 来改变来自 Django Data 的一些手风琴的打开和关闭,数据显示正确,当我点击另一个元素时,其余的手风琴关闭,没关系,但是当我再次点击这个手风琴的同一个头部时,这个不关就重新开一次。

$(document).on('click', '.accordion-head', function (e) {

    e.preventDefault();

    var value = $(this).children().children().attr("id");
    $("#paquete").val("Paquete seleccionado: " + value);

    $('.accordion-head').children().removeClass('active');
    $('.accordion-head').children().children().children().removeClass(' iluminar');
    $('.accordion-body').slideUp();
    $('.accordion-head .pull-right').html('+');
    $(this).next().slideDown();
    $(this).children().addClass('active');
    $(this).children().children().children().addClass(' iluminar');
    $(this).children().children().children().children().html('-');
    return false;
});

I desire that when I click in the head of the open body, the body close, and the head return to first class color (just for example, the initial color was green and after then orange).我希望当我点击打开身体的头部时,身体关闭,头部回到一流的颜色(例如,最初的颜色是绿色,然后是橙色)。

example:例子:

jsfiddle提琴手

You can check whether the accordion you clicked has class active using hasClass() method.您可以使用hasClass()方法检查您单击的手风琴是否具有active类。 Here is the updated code:这是更新后的代码:

$(document).on('click', '.accordion-head', function (e) {
    e.preventDefault();

    var value = $(this).children().children().attr("id");
    $("#paquete").val("Paquete seleccionado: " + value);

    $(this).children('a.iluminar').toggleClass('active');
    $('.accordion-head h3').removeClass('iluminar');
    $('.accordion-head .pull-right').html('+');
    $('.accordion-body').slideUp();

    if ($(this).children('a.iluminar').hasClass('active')) {
        $(this).next().slideDown();
        $(this).find('h3').addClass('iluminar');
        $(this).find('.pull-right').html('-');
    }
});

Here is the fiddle based on your code.这是基于您的代码的小提琴

I hope this works as you expect.我希望这能如你所愿。

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

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