Collapsible accordion doesn't work inside a dr

2019-09-04 15:29发布

I want to mix a dropdown and collapse menu together, but when I implent an accordion inside a dropdown-menu you can not click on the accordion toggle. The dropdown menu slides up.

Here is my code.

<ul class="nav nav-pills">
  <li class="dropdown" id="menu1">
    <a class="dropdown-toggle" data-toggle="dropdown" href="#menu1">
      Dropdown
      <b class="caret"></b>
    </a>

    <ul class="dropdown-menu">

      <li>
        <a href="#">Action</a>
      </li>

      <li class="divider"></li>

      <li>
        <a href="#">Separated link</a>
      </li>

      <li>
        <div class="accordion" id="accordion2">  

          <div class="accordion-group"> 

            <a class="accordion-toggle" 
               data-toggle="collapse" 
               data-parent="#accordion2" 
               href="#demo1">

               aaa

            </a> 

            <div id="demo1" class="accordion-body collapse" style="height: 0px; ">  
              <ul class="index-list">
                 ......
              </ul>              
            </div>                
          </div>   

          <div class="accordion-group">  

            <a class="accordion-toggle" 
               data-toggle="collapse" 
               data-parent="#accordion2" 
               href="#demo2">

               aaa

            </a>   

            <div id="demo2" class="accordion-body collapse">  
              <ul class="index-list">
               ......
              </ul>             
            </div>               
          </div>  
        </div>
      </li>
    </ul>
  </li>
  ...
</ul>​

Here's a link to the jsFiddle: http://jsfiddle.net/DBQU7/26/

1条回答
贼婆χ
2楼-- · 2019-09-04 16:13

I don't think that accordions were meant to be included into dropdowns.

I think you have to trigger the accordion yourself, and avoid the extra markup that accordions would normally require. That way you can keep the dropdown style.

Here is what i came up with.

Live example : http://jsfiddle.net/Sherbrow/YqsTw/

<ul class="nav nav-pills">
    <li class="dropdown" id="menu1">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#menu1">
    Dropdown
            <b class="caret"></b>
        </a>
        <ul class="dropdown-menu">
            <li>
                <a href="#">Action</a>
            </li>
            <li class="divider"/>
            <li>
                <a href="#">Another action</a>
            </li>
            <li class="divider"/>
            <li>
                <a href="#collapsible1" class="accordion-toggle" data-parent="#menu1">Accordion 1</a>
            </li>
            <li class="collapse in" id="collapsible1">
                <a href="#">Collapsible 1</a>
            </li>
            <li class="divider"/>
            <li>
                <a href="#" data-target=".collapse2" class="accordion-toggle" data-parent="#menu1">Accordion 2</a>
            </li>
            <li class="collapse collapse2">
                <a href="#">Collapsible 2</a>
            </li>
            <li class="collapse collapse2">
                <a href="#">Collapsible 2 bis</a>
            </li>
        </ul>
    </li>
</ul>
$('.dropdown-toggle').dropdown();
$('.dropdown-menu').each(function() {
    $(this).on('click', '.accordion-toggle', function(event) {
        event.stopPropagation();
        var $this = $(this);

        var parent = $this.data('parent');
        var actives = parent && $(parent).find('.collapse.in');

        // From bootstrap itself
        if (actives && actives.length) {
            hasData = actives.data('collapse');
            if (hasData && hasData.transitioning) return;
            actives.collapse('hide');
        }

        var target = $this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, ''); //strip for ie7

        $(target).collapse('toggle');
    });
});
查看更多
登录 后发表回答