Jquery 没有关闭下拉菜单

Posted

技术标签:

【中文标题】Jquery 没有关闭下拉菜单【英文标题】:Jquery is not closing drop-down 【发布时间】:2021-05-11 12:39:55 【问题描述】:

我正在使用 Bootstrap 导航栏构建多级(仅第二级)下拉菜单。我可以找到一个guide 来构建多级下拉菜单。

JSFiddle link

但是,下拉菜单不会在点击时关闭。点击Document可以关闭下拉菜单,我在jquery下面添加了。

$(document).click(function()
    $('.dropdown-menu').removeClass('show');
);

下拉菜单关闭,但在其他nav-item 中单击时,打开的下拉菜单不会关闭。所以,我缺少基本的JQuery。 基本上show 类在单击其他nav-item 时不会从.dropdown-menu 类中删除。所以,我想从.dropdown-menu 中删除show

编辑:我正在寻找工作 JSJquery 代码,上面的 Jquery 是从 staoverflow 复制的损坏代码。所以,寻找完整和干净的解决方案

$('.dropdown-menu a.dropdown-toggle').on('click', function(e) 
    if (!$(this).next().hasClass('show')) 
      $(this).parents('.dropdown-menu').first().find('.show').removeClass('show');
    
    var $subMenu = $(this).next('.dropdown-menu');
    $subMenu.toggleClass('show');
  
    return false;
  );

$(document).click(function() 
    //   $('.dropdown-menu').toggleClass('show');
$('.dropdown-menu').removeClass('show');
);

$('.dropdown').click(function(event)
  event.stopPropagation();
);
.dropdown-submenu 
    position: relative;
  
  
  .dropdown-submenu a::after 
    transform: rotate(-90deg);
    position: absolute;
    right: 6px;
    top: .8em;
  
  
  .dropdown-submenu .dropdown-menu 
    top: 0;
    left: 100%;
    margin-left: .1rem;
    margin-right: .1rem;
  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-md navbar-dark bg-primary py-1">
        <div class="container-fluid">
        <div class="flex-row d-flex">         
            <a class="navbar-brand" href="#">Brand</a>
        </div>    
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button> 
<div class="navbar-collapse collapse" id="navbarSupportedContent">
            <ul class="navbar-nav">
                <li class="nav-item dropdown">
                  <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                    Dogs
                  </a>
                  <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                    <li><a class="dropdown-item" href="#">Bulldog</a></li>
                    <li><a class="dropdown-item" href="#">Al</a></li>
                    <li><a class="dropdown-item" href="#">Labrador</a></li>
                    <li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" href="#">Puppies</a>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="#">Husky</a></li>
                            <li><a class="dropdown-item" href="#">Husky B</a></li>
                            <li><a class="dropdown-item" href="#">Husky C</a></li>
                          </ul>
                    </li>
                    <li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" href="#">Pet Products</a>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="#">Cat</a></li>
                            <li><a class="dropdown-item" href="#">Dogs</a></li>
                            <li><a class="dropdown-item" href="#">Reptile</a></li>
                            <li><a class="dropdown-item" href="#">Amphibian</a></li>
                          </ul>
                    </li>
                  </ul>
                </li>
                  <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                      Pet Cages
                    </a>
                    <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                      <li><a class="dropdown-item" href="#">Cat</a></li>
                      <li><a class="dropdown-item" href="#">Dogs</a></li>
                      <li><a class="dropdown-item" href="#">Reptile</a></li>
                    </ul>
                  </li>
                  <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                      Pet Guides
                    </a>
                    <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                      <li><a class="dropdown-item" href="#">Labrador</a></li>
                      <li><a class="dropdown-item" href="#">Cat</a></li>
                      <li><a class="dropdown-item" href="#">Lizard</a></li>
                    </ul>
                  </li>
                  <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                      ML & AI
                    </a>
                    <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                      <li><a class="dropdown-item" href="#">Pet CBD</a></li>                      
                    </ul>
                  </li>
                <!-- <form class="d-flex"> -->
                    <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
                    <!-- <button class="btn btn-outline-success" type="submit">Search</button> -->
                  <!-- </form> -->
              </ul>
        </div>
        </div>
        </nav>

【问题讨论】:

@user1735921 在父元素#navbarSupportedContent的类列表中添加show 当我点击其他导航项时它会为我关闭。 如果您不使用其他编辑器,请在 JSFiddle 中尝试 @munna01 您在问题中的完整示例代码集应该完全重现您的问题。在 Chrome 中进行测试,这个 sn-p 似乎可以正常工作。请在该特定代码集中提供有关您的问题的更多详细信息或特定信息,作为您实际问题的一部分。 (不是小提琴,这里是这个) @MarkSchultheiss 请检查,我已经添加了 gif 截图(解释问题 - “狗下拉菜单应该关闭点击宠物笼下拉菜单” 【参考方案1】:

对于 持久性打开的 DROPWON 的主要问题,您可以通过侦听 nav-link 事件 show.bs.dropdown 来解决该问题,然后从 .dropdown 包装的 .dropdown-submenu 中删除 show 类

如下:

$('.nav-link').on('show.bs.dropdown', function(e) 
  $(".dropdown-submenu .dropdown-menu").removeClass("show");
);

请参阅下面的工作 sn-p :(也可以全屏尝试)

为了测试,我从 sn-p 中删除了一些菜单:

$('.dropdown-menu a.dropdown-toggle').on('click', function(e) 
  if (!$(this).next().hasClass('show')) 
    $(this).parents('.dropdown-menu').first().find('.show').removeClass('show');
  
  var $subMenu = $(this).next('.dropdown-menu');
  $subMenu.toggleClass('show');

  return false;
);


$('.nav-link').on('show.bs.dropdown', function(e) 
  $(".dropdown-submenu .dropdown-menu").removeClass("show");
);

$(document).on("click", ".nav-link", function(e)
  console.log("clicl nav");
)

$('.dropdown').click(function(event) 
  console.log("click");
  event.stopPropagation();
);
.dropdown-submenu 
  position: relative;


.dropdown-submenu a::after 
  transform: rotate(-90deg);
  position: absolute;
  right: 6px;
  top: .8em;


.dropdown-submenu .dropdown-menu 
  top: 0;
  left: 100%;
  margin-left: .1rem;
  margin-right: .1rem;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-expand-md navbar-dark bg-primary py-1">
  <div class="container-fluid">
    <div class="flex-row d-flex">
      <a class="navbar-brand" href="#">Brand</a>
    </div>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
    <div class="navbar-collapse collapse" id="navbarSupportedContent">
      <ul class="navbar-nav">
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                    Dogs
                  </a>
          <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
            <li><a class="dropdown-item" href="#">Bulldog</a></li>
            <li><a class="dropdown-item" href="#">Al</a></li>
            <li><a class="dropdown-item" href="#">Labrador</a></li>
            <li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" href="#">Puppies</a>
              <ul class="dropdown-menu">
                <li><a class="dropdown-item" href="#">Husky</a></li>
                <li><a class="dropdown-item" href="#">Husky B</a></li>
                <li><a class="dropdown-item" href="#">Husky C</a></li>
              </ul>
            </li>
            <li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" href="#">Pet Products</a>
              <ul class="dropdown-menu">
                <li><a class="dropdown-item" href="#">Cat</a></li>
                <li><a class="dropdown-item" href="#">Dogs</a></li>
                <li><a class="dropdown-item" href="#">Reptile</a></li>
                <li><a class="dropdown-item" href="#">Amphibian</a></li>
              </ul>
            </li>
          </ul>
        </li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                      Pet Cages
                    </a>
          <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
            <li><a class="dropdown-item" href="#">Cat</a></li>
            <li><a class="dropdown-item" href="#">Dogs</a></li>
            <li><a class="dropdown-item" href="#">Reptile</a></li>
          </ul>
        </li>
        <!-- <button class="btn btn-outline-success" type="submit">Search</button> -->
        <!-- </form> -->
      </ul>
    </div>
  </div>
</nav>

【讨论】:

@munna01,你能准确描述一下你需要什么吗?单击外部时关闭下拉菜单? 是的。点击甚至其他导航链接,应该关闭以前打开的导航(side-nav)s 能否请您调查一下 - 这个问题,提出了新问题:***.com/questions/66098848/… @munna01 ,更新了我的答案,我想现在我可以理解你的问题了(在看到屏幕记录 gif 之后) 是我的错误,我不能写得更好来说服你。可能是我英语不好(是这个原因)【参考方案2】:

您无需添加jQuery 即可实现此目的。 删除jQuery 和额外的javascript 代码,您的下拉菜单将通过添加一个简单的css 代码来工作。

        .dropdown-submenu 
            position: relative;
        
        
        .dropdown-submenu > .dropdown-menu 
            top: 0;
            left: 100%;
            margin-top: -6px;
            margin-left: -1px;
            -webkit-border-radius: 0 6px 6px 6px;
            -moz-border-radius: 0 6px 6px;
            border-radius: 0 6px 6px 6px;
        
        
        .dropdown-submenu:hover > .dropdown-menu 
            display: block;
        
        
        .dropdown-submenu>a : after 
            display: block;
            content: " ";
            float: right;
            width: 0;
            height: 0;
            border-color: transparent;
            border-style: solid;
            border-width: 5px 0 5px 5px;
            border-left-color: #ccc;
            margin-top: 5px;
            margin-right: -10px;
        
        
        .dropdown-submenu:hover > a : after 
            border-left-color: #fff;
        
        
        .dropdown-submenu.pull-left 
            float: none;
        
        
        .dropdown-submenu.pull-left > .dropdown-menu 
            left: -100%;
            margin-left: 10px;
            -webkit-border-radius: 6px 0 6px 6px;
            -moz-border-radius: 6px 0 6px 6px;
            border-radius: 6px 0 6px 6px;
        
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-md navbar-dark bg-primary py-1">
        <div class="container-fluid">
        <div class="flex-row d-flex">         
            <a class="navbar-brand" href="#">Brand</a>
        </div>    
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button> 
<div class="navbar-collapse collapse" id="navbarSupportedContent">
            <ul class="navbar-nav">
                <li class="nav-item dropdown">
                  <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                    Dogs
                  </a>
                  <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                    <li><a class="dropdown-item" href="#">Bulldog</a></li>
                    <li><a class="dropdown-item" href="#">Al</a></li>
                    <li><a class="dropdown-item" href="#">Labrador</a></li>
                    <li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" href="#">Puppies</a>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="#">Husky</a></li>
                            <li><a class="dropdown-item" href="#">Husky B</a></li>
                            <li><a class="dropdown-item" href="#">Husky C</a></li>
                          </ul>
                    </li>
                    <li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" href="#">Pet Products</a>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="#">Cat</a></li>
                            <li><a class="dropdown-item" href="#">Dogs</a></li>
                            <li><a class="dropdown-item" href="#">Reptile</a></li>
                            <li><a class="dropdown-item" href="#">Amphibian</a></li>
                          </ul>
                    </li>
                  </ul>
                </li>
                  <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                      Pet Cages
                    </a>
                    <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                      <li><a class="dropdown-item" href="#">Cat</a></li>
                      <li><a class="dropdown-item" href="#">Dogs</a></li>
                      <li><a class="dropdown-item" href="#">Reptile</a></li>
                    </ul>
                  </li>
                  <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                      Pet Guides
                    </a>
                    <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                      <li><a class="dropdown-item" href="#">Labrador</a></li>
                      <li><a class="dropdown-item" href="#">Cat</a></li>
                      <li><a class="dropdown-item" href="#">Lizard</a></li>
                    </ul>
                  </li>
                  <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                      ML & AI
                    </a>
                    <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                      <li><a class="dropdown-item" href="#">Pet CBD</a></li>                      
                    </ul>
                  </li>
                <!-- <form class="d-flex"> -->
                    <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
                    <!-- <button class="btn btn-outline-success" type="submit">Search</button> -->
                  <!-- </form> -->
              </ul>
        </div>
        </div>
        </nav>

【讨论】:

不能接受悬停,这会破坏移动网站。

以上是关于Jquery 没有关闭下拉菜单的主要内容,如果未能解决你的问题,请参考以下文章

关闭多级 jquery 下拉菜单

带有多个子菜单下拉菜单的 jQuery 导航菜单关闭父菜单项

通过 javascript/jquery 关闭 select2 下拉菜单

Rails + JQuery 侧边栏下拉菜单立即关闭

在内部单击时保持 Bootstrap 下拉菜单打开

jquery实现下拉