每个下拉选项中的手风琴内容无效

问题描述 投票:0回答:2

我想在每个下拉列表中创建一个手风琴,但它不能正常工作。第一个下拉选项中显示每个下拉选项的所有手风琴,如果我选择另一个下拉选项,则单击时无法打开手风琴。如何解决这个问题?

这是link for full snippet

$(function() {
  $('#faqDrodpown').change(function(){
    $('.faqs').hide();
    $('#' + $(this).val()).show();
  });

  $('#accordion-faq').find('.accordionfaq-toggle').click(function() { /*Expand or collapse this panel*/
        $(this).next().slideToggle('fast');
        $(".accordionfaq-content").not($(this).next()).slideUp('fast');
    });
});
javascript jquery drop-down-menu accordion dropdown
2个回答
1
投票

刚刚将jquery $('#accordion-faq').find('.accordionfaq-toggle')改为$('#accordion-faq .accordionfaq-toggle'),它的工作对我来说很好

编辑:更改了$('#faqDrodpown').change()函数,切换类hide并在该类上添加了用于隐藏内容的css以及用于隐藏其他faq块的onload我添加了一些js代码

$(function() {
  $('.faqs').addClass('hide');
  $('#' + $('#faqDrodpown').val()).removeClass('hide');
    
  $('#faqDrodpown').change(function(){
    $('.faqs').addClass('hide');
    $('#' + $(this).val()).removeClass('hide');
  });
  
  $('#accordion-faq .accordionfaq-toggle').click(function(){ /*Expand or collapse this panel*/
        $(this).next('.accordionfaq-content').slideToggle('fast');
         $(".accordionfaq-content").not($(this).next()).slideUp('fast');
    });
});
.faq-dropdown {
  position: relative;
  display:block;
  margin-top:0.5em;
  padding:0;
}
.faq-dropdown select {
  width:100%;
  margin:0;
  background:none;
  border: 1px solid transparent;
  outline: none;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  appearance: none;
  -webkit-appearance: none;
  font-size:1.25em;
  color: #444;
  padding: .6em 1.9em .5em .8em;
  line-height:1.3;
}

.faq-dropdown::after {
  content: "";
  position: absolute;
  width: 9px;
  height: 8px;
  top: 50%;
  right: 1em;
  margin-top:-4px;
  z-index: 2;
  background: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 12'%3E%3Cpolygon fill='rgb(102,102,102)' points='8,12 0,0 16,0'/%3E%3C/svg%3E") 0 0 no-repeat;  
  pointer-events:none;
}

/* This hides native dropdown button arrow in IE 10/11+ so it will have the custom appearance, IE 9 and earlier get a native select */
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
  .faq-dropdown select::-ms-expand {
    display: none;
  }
  /* Removes the odd blue bg color behind the text in IE 10/11 and sets the text to match the focus style text */
  select:focus::-ms-value {
    background: transparent;
    color: #222;
  }
}

/* Firefox 7+ -- Will let us hide the arrow, but inconsistently (see FF 30 comment below). We've found the simplest way to hide the native styling in FF is to make the select bigger than its container. */
/* The specific FF selector used below successfully overrides the previous rule that turns off the custom icon; other FF hacky selectors we tried, like `*>.dropdown::after`, did not undo the previous rule */

/* Set overflow:hidden on the wrapper to clip the native select's arrow, this clips hte outline too so focus styles are less than ideal in FF */
_::-moz-progress-bar, body:last-child .dropdown {
  overflow: hidden;
}
/* Show only the custom icon */
_::-moz-progress-bar, body:last-child .dropdown:after {
  display: block;
}
_::-moz-progress-bar, body:last-child .dropdown select {
  /* increase padding to make room for menu icon */
  padding-right: 1.9em;
  /* `window` appearance with these text-indent and text-overflow values will hide the arrow FF up to v30 */
  -moz-appearance: window;
  text-indent: 0.01px;
  text-overflow: "";
  /* for FF 30+ on Windows 8, we need to make the select a bit longer to hide the native arrow */
  width: 110%;
}

/* At first we tried the following rule to hide the native select arrow in Firefox 30+ in Windows 8, but we'd rather simplify the CSS and widen the select for all versions of FF since this is a recurring issue in that browser */
/* @supports (-moz-appearance:meterbar) and (background-blend-mode:difference,normal) {
.dropdown select { width:110%; }
}   */


/* Firefox 7+ focus style - This works around the issue that -moz-appearance: window kills the normal select focus. Using semi-opaque because outline doesn't handle rounded corners */
_::-moz-progress-bar, body:last-child .dropdown select:focus {
  outline: 2px solid rgba(180,222,250, .7);
}


/* Opera - Pre-Blink nix the custom arrow, go with a native select button */
x:-o-prefocus, .dropdown::after {
  display:none;
}


/* Hover style */
.faq-dropdown:hover {
  border:1px solid #888;
}

/* Focus style */
select:focus {
  outline:none;
  box-shadow: 0 0 1px 3px rgba(180,222,250, 1);
  background-color:transparent;
  color: #222;
  border:1px solid #aaa;
}


/* Firefox focus has odd artifacts around the text, this kills that */
select:-moz-focusring {
  color: transparent;
  text-shadow: 0 0 0 #000;
}

option {
  font-weight:normal;
}


/* These are just demo button-y styles, style as you like */
.faq-dropdown-btn {
  border: 1px solid #bbb;
  border-radius: .3em;
  box-shadow: 0 1px 0 1px rgba(0,0,0,.04);
  background: #f3f3f3; /* Old browsers */
  background: -moz-linear-gradient(top, #ffffff 0%, #e5e5e5 100%); /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color-stop(100%,#e5e5e5)); /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #ffffff 0%,#e5e5e5 100%); /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #ffffff 0%,#e5e5e5 100%); /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #ffffff 0%,#e5e5e5 100%); /* IE10+ */
  background: linear-gradient(to bottom, #ffffff 0%,#e5e5e5 100%); /* W3C */
}

.output {
  margin: 0 auto;
  padding: 1em; 
}

.accordionfaq-toggle {
        cursor: pointer;
        font-size: 1.1em;
        font-weight: 700;
        text-align: left;
        border-radius: 3px;
        padding: 19px 19px 19px 60px;
        border: 1px solid #CCC;
        box-shadow: 0 0 5px rgba(0, 0, 0, .19), 0 3px 3px rgba(0, 0, 0, .23);
        background: #FFF;
        border-radius: 3px;
    }
.accordionfaq-toggle:active {
        border-bottom: none;
    }
    
    .accordionfaq-content {
        display: none;
        border-radius: 3px;
        border: 1px solid #CCC;
        border-top: 0 !important;
        background: #FFF;
        box-shadow: 0 0 5px rgba(0, 0, 0, .19), 0 3px 3px rgba(0, 0, 0, .23);
        margin: -87px 0 19px 0;
        padding: 4em 3em 3em 4em;
    }
    
    .accordionfaq-content>p {
        margin-top: 2.5em;
    }
    
    .faqs.hide {
      display:none;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="faq-dropdown-btn faq-dropdown"> 
  <select id="faqDrodpown">
     <option value="red">Red</option>
     <option value="yellow">Yellow</option>
     <option value="blue">Blue</option>
  </select>
</div>

<div class="output">
  <div id="red" class="faqs">
    <div id="accordion-faq">
    <h4 class="accordionfaq-toggle"><span class="arrow">Lorem ipsum title</span></h4>
  <div class="accordionfaq-content">Lorem ipsum content</div>
    
  </div>
  </div>
  <div id="yellow" class="faqs">
    <div id="accordion-faq">
    <h4 class="accordionfaq-toggle"><span class="arrow">Lorem ipsum title</span></h4>
  <div class="accordionfaq-content">Lorem ipsum content</div>
    <h4 class="accordionfaq-toggle"><span class="arrow">Lorem ipsum title</span></h4>
  <div class="accordionfaq-content">Lorem ipsum content</div>
    </div>
  </div>
  <div id="blue" class="faqs">
  <div id="accordion-faq">
    <h4 class="accordionfaq-toggle"><span class="arrow">Lorem ipsum title</span></h4>
  <div class="accordionfaq-content">Lorem ipsum content</div>
    <h4 class="accordionfaq-toggle"><span class="arrow">Lorem ipsum title</span></h4>
  <div class="accordionfaq-content">Lorem ipsum content</div>
    <h4 class="accordionfaq-toggle"><span class="arrow">Lorem ipsum title</span></h4>
  <div class="accordionfaq-content">Lorem ipsum content</div>
  </div>
  </div>
</div>

0
投票

将JS改为

$(function() {

  $('#faqDrodpown').change(function(){
    $('.faqs').hide();
    $('#' + $(this).val()).show();
    var data = $("#faqDrodpown").find("option:selected").val();
    $('#accordion-faq-'+data).find('.accordionfaq-toggle').click(function() { 
          $(this).next().slideToggle('fast');
          $(".accordionfaq-content").not($(this).next()).slideUp('fast');
      });
  });
});

将HTML更改为

<div id="accordion-faq-red">
<div id="accordion-faq-yellow">
<div id="accordion-faq-blue">
© www.soinside.com 2019 - 2024. All rights reserved.