【问题标题】:Accordion content inside each dropdown option not working每个下拉选项中的手风琴内容不起作用
【发布时间】:2018-09-14 05:34:06
【问题描述】:

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

这是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


    【解决方案1】:

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

    编辑: 更改了 $('#faqDrodpown').change() 切换类 hide 的函数 并在该类上添加 css 以隐藏内容和 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>

    【讨论】:

    • 我明白了,但是当您第一次访问下拉菜单所在的页面时,所有手风琴都会显示,但应该不会。看看下拉选项“红色”,它只有1个手风琴,但它显示了其他选项的所有手风琴,但是当重新选择它时,它显示正确。
    • 我已经按照你说的更改了我的代码检查新的更新代码
    • 我尝试应用更新后的 sn-p,但仍然显示在第一个选项内容上的其他选项的所有手风琴,codepen.io/anon/pen/bxjNZO
    • 您是否已将hide 类添加到除第一个以外的所有faqs 块中?
    • 或者你可以调用`$('.faqs').addClass('hide'); $('#red').removeClass('hide');` 在 jquery 中pageload
    【解决方案2】:

    把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">
    

    【讨论】:

      猜你喜欢
      • 2019-05-15
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      • 2012-06-26
      • 2013-01-20
      • 2019-01-16
      相关资源
      最近更新 更多