Bootstrap & jQuery - 根据 Bootstrap 下拉菜单打开或关闭 Div

Bootstrap & jQuery - Toggle Div based on Bootstrap Dropdown Menu open or close

我想在 Bootstrap 下拉菜单打开时显示 div 并在下拉菜单关闭时隐藏它...


FIDDLE


HTML


<ul class="dropdown" id="flatmenu">
  <li data-toggle="dropdown" aria-expanded="true"><a href="#">Dropdown<span class="caret"></span></a></li>
  <div class="dropdown-menu fm-container" role="menu" aria-labelledby="dropdownMenu">
    Dropdown Content
  </div>
</ul>


<div class="show-me">Show Me</div>

CSS


body{width:400px;margin:50px auto;}
ul,li{list-style:none;margin:0;padding:0;}
.show-me{display:none;margin-top:200px;}

jQuery


if($('#flatmenu').hasClass('open')){
    $('.show-me').show('slow');
}else{
    $('.show-me').hide('slow');
}

来自bootstrap-docs

$('#flatmenu').on('shown.bs.dropdown hidden.bs.dropdown', function () {
  $('.show-me').toggle('slow');
});

Demo

您可以对即将 show/hide 的 div 执行 .click() for the element that it's intended for and use the .toggle() 效果。

代码片段:

$("#flatmenu").click(function () {
    $('.show-me').toggle('slow');
});

JsFiddle demo

你可以试试这个更新后的fiddle

代码:

$('#flatmenu').on('click',function(){
    $this = $(this);
    setTimeout(function(){
        if($this.hasClass('open')){
            $('.show-me').show('slow');
        }else{
            $('.show-me').hide('slow');
        }
    },100);
});