强制手风琴关闭所有标签

时间:2015-12-11 22:40:40

标签: javascript jquery jquery-ui accordion

我在问这个问题之前试图寻找解决方案,但不幸的是,我无法对我的代码做出任何回答!

我想强制关闭所有手风琴标签,以便用户可以根据自己的喜好打开或关闭。

我的代码是:

jQuery(".tap-title").each(function(){
    jQuery(this).click(function() {
        if (jQuery(this).parent().parent().hasClass("toggle-accordion")) {
            jQuery(this).parent().find("li:first .tap-title").addClass("active");
            jQuery(this).parent().find("li:first .tap-title").next(".accordion-inner").addClass("active");
            jQuery(this).toggleClass("active");
            jQuery(this).next(".accordion-inner").slideToggle().toggleClass("active");
            jQuery(this).find("i").toggleClass("fa-plus").toggleClass("fa-minus");
        }else {
            if (jQuery(this).next().is(":hidden")) {
                jQuery(this).parent().parent().find(".tap-title").removeClass("active").next().slideUp(200);
                jQuery(this).parent().parent().find(".tap-title").next().removeClass("active").slideUp(200);
                jQuery(this).toggleClass("active").next().slideDown(200);
                jQuery(this).next(".accordion-inner").toggleClass("active");
                jQuery(this).parent().parent().find("i").removeClass("fa-minus").addClass("fa-plus");
                jQuery(this).find("i").removeClass("fa-plus").addClass("fa-minus");
            }
        }
        return false;

    });
});

我的HTML代码是:

    <div class="col-md-6">
        <div class="title-section text-left">
            <h4 class="bold">Normal Horizontal Tabs</h4>
        </div>      
        <scrip> 
        <div class="accordions-style1">
            <div class="section-content">
                <h4 class="tap-title active"><a href="#">What Is Yamen?<i class="fa fa-minus"></i></a></h4>
                <div class="tap-inner" style="display: block;">
                    <p>Duis dapibus aliquam mi, eget euismod scelerisque ut. Vivamus at elit quis urna adipiscing , Curabitur vitae velit in neque dictum blandit. Duis dapibus aliquam mi, eget euismod sceler ut.Duis dapibus aliquam mi, eget euismod scelerisque at elit quis urna adipiscing , Curabitur vitae velit in neque dictum blandit. Duis dapibus aliquam mi, eget euismod sceler ut.</p>
                </div>
            </div>
            <div class="section-content">
                <h4 class="tap-title"><a href="#">Why Should I purchase this template?<i class="fa fa-plus"></i></a></h4>
                <div class="tap-inner" style="display: none;">
                    <p>Duis dapibus aliquam mi, eget euismod scelerisque ut. Vivamus at elit quis urna adipiscing , Curabitur vitae velit in neque dictum blandit. Duis dapibus aliquam mi, eget euismod sceler ut.Duis dapibus aliquam mi, eget euismod scelerisque at elit quis urna adipiscing , Curabitur vitae velit in neque dictum blandit. Duis dapibus aliquam mi, eget euismod sceler ut.</p>
                </div>
            </div>
            <div class="section-content">
                <h4 class="tap-title"><a href="#">What about features?<i class="fa fa-plus"></i></a></h4>
                <div class="tap-inner" style="display: none;">
                    <p>Duis dapibus aliquam mi, eget euismod scelerisque ut. Vivamus at elit quis urna adipiscing , Curabitur vitae velit in neque dictum blandit. Duis dapibus aliquam mi, eget euismod sceler ut.Duis dapibus aliquam mi, eget euismod scelerisque at elit quis urna adipiscing , Curabitur vitae velit in neque dictum blandit. Duis dapibus aliquam mi, eget euismod sceler ut.</p>
                </div>
            </div>
        </div>
    </div>

4 个答案:

答案 0 :(得分:1)

我在@ mohamed-yousef的帮助下找到了我的问题的答案,非常感谢你的配偶。

我用这个替换了我的jQuery代码:

jQuery(".tap-title").each(function(){
    jQuery(this).click(function() {
        if (jQuery(this).parent().parent().hasClass("toggle-accordion")) {
            jQuery(this).parent().find("li:first .tap-title").addClass("active");
            jQuery(this).parent().find("li:first .tap-title").next(".accordion-inner").addClass("active");
            jQuery(this).toggleClass("active");
            jQuery(this).next(".accordion-inner").slideToggle().toggleClass("active");
            jQuery(this).find("i").toggleClass("fa-plus").toggleClass("fa-minus");
        }else {
            if (jQuery(this).next().is(":hidden")) {
                jQuery(this).parent().parent().find(".tap-title").removeClass("active").next().slideUp(200);
                jQuery(this).parent().parent().find(".tap-title").next().removeClass("active").slideUp(200);
                jQuery(this).toggleClass("active").next().slideDown(200);
                jQuery(this).next(".accordion-inner").toggleClass("active");
                jQuery(this).parent().parent().find("i").removeClass("fa-minus").addClass("fa-plus");
                jQuery(this).find("i").removeClass("fa-plus").addClass("fa-minus");
        }else{
                jQuery(this).toggleClass("active").next().slideToggle(200);
                jQuery(this).next(".accordion-inner").toggleClass("active");
                jQuery(this).parent().parent().find("i").removeClass("fa-minus").addClass("fa-plus");
        }
        }
        return false;
    });
});

用这个代替我的HTML代码:

    <div class="col-md-6">
        <div class="title-section text-left">
            <h4 class="bold">Normal Horizontal Tabs</h4>
        </div>      
        <scrip> 
        <div class="accordions-style1">
            <div class="section-content">
                <h4 class="tap-title"><a href="#">What Is Yamen?<i class="fa fa-plus"></i></a></h4>
                <div class="tap-inner" style="display: none;">
                    <p>Duis dapibus aliquam mi, eget euismod scelerisque ut. Vivamus at elit quis urna adipiscing , Curabitur vitae velit in neque dictum blandit. Duis dapibus aliquam mi, eget euismod sceler ut.Duis dapibus aliquam mi, eget euismod scelerisque at elit quis urna adipiscing , Curabitur vitae velit in neque dictum blandit. Duis dapibus aliquam mi, eget euismod sceler ut.</p>
                </div>
            </div>
            <div class="section-content">
                <h4 class="tap-title"><a href="#">Why Should I purchase this template?<i class="fa fa-plus"></i></a></h4>
                <div class="tap-inner" style="display: none;">
                    <p>Duis dapibus aliquam mi, eget euismod scelerisque ut. Vivamus at elit quis urna adipiscing , Curabitur vitae velit in neque dictum blandit. Duis dapibus aliquam mi, eget euismod sceler ut.Duis dapibus aliquam mi, eget euismod scelerisque at elit quis urna adipiscing , Curabitur vitae velit in neque dictum blandit. Duis dapibus aliquam mi, eget euismod sceler ut.</p>
                </div>
            </div>
            <div class="section-content">
                <h4 class="tap-title"><a href="#">What about features?<i class="fa fa-plus"></i></a></h4>
                <div class="tap-inner" style="display: none;">
                    <p>Duis dapibus aliquam mi, eget euismod scelerisque ut. Vivamus at elit quis urna adipiscing , Curabitur vitae velit in neque dictum blandit. Duis dapibus aliquam mi, eget euismod sceler ut.Duis dapibus aliquam mi, eget euismod scelerisque at elit quis urna adipiscing , Curabitur vitae velit in neque dictum blandit. Duis dapibus aliquam mi, eget euismod sceler ut.</p>
                </div>
            </div>
        </div>
    </div>

答案 1 :(得分:0)

首先,您可以将jquery()函数替换为$(),例如$('.tap-title').each(function(){...});)

其次,虽然我不太了解你的HTML结构,因为你没有提供你的问题,但从它的外观来看,它看起来好像你正在用类切换手风琴? / p>

所以我会按照.active类的目标做一些事情,然后删除自己的.active类并将其滑动?提供HTML代码对我有很大帮助,但希望这会有所帮助。

答案 2 :(得分:0)

假设您正在使用jquery-ui手风琴,我相信这会奏效:

$('.ui-accordion-header').removeClass('ui-accordion-header-active ui-state-active ui-corner-top').addClass('ui-corner-all').attr({
    'aria-selected': 'false',
    'tabindex': '-1'
});
$('.ui-accordion-header-icon').removeClass(icons.headerSelected).addClass(icons.header);
$('.ui-accordion-content').removeClass('ui-accordion-content-active').attr({
    'aria-expanded': 'false',
    'aria-hidden': 'true'
 }).hide();

答案 3 :(得分:0)

基本手风琴:

<强> HTML:

<div class="accordion">
  <div class="accordion-head">
    title here
  </div>
  <div class="accordion-body">
    content here
  </div>
</div>

<强> CSS:

.accordion-body{
   display:none;
}

jQuery (或$()而不是jQuery())

jQuery(document).ready(function(){

  var items = jQuery('.accordion');
  var heads = items.find('.accordion-head);


  heads.click(function(){

     var i = jQuery(this);

     items.find('.accordion-body').slideUp(); // close all accordions
     i.next('.accordion-body').slideDown();


  });

});