简体   繁体   English

Bootstrap 4:单击下拉项时如何关闭下拉菜单?

[英]Bootstrap 4: How Do I Close a dropdown-menu when a dropdown-item is clicked?

I have a Bootstrap dropdown menu in a navbar.我在导航栏中有一个 Bootstrap 下拉菜单。 You need to click the menu to open it.您需要单击菜单将其打开。 If you click outside of the menu, it will close.如果您在菜单外单击,它将关闭。 If you click one of the items (class dropdown-item), it remains open.如果您单击其中一项(类下拉项),它会保持打开状态。 I would like to close it when a menu item is clicked.我想在单击菜单项时关闭它。 All of the items I have found are referencing earlier version of Bootstrap.我发现的所有项目都引用了早期版本的 Bootstrap。

Here is my code.这是我的代码。 The first time I click a menu item when I enter the website, the menu closes.当我进入网站时第一次单击菜单项时,菜单关闭。 The next time I click a menu item it remains open.下次我单击菜单项时,它仍保持打开状态。

<li class="nav-item dropdown">
  <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <%= "#{t :link_products}" %>
  </a>
  <div class="dropdown-menu" aria-labelledby="navbarDropdown">
    <div class="dropdown-item"><%= link_to "#{t :link_app}", locale_root_path(anchor: "games"), class: "page-scroll" %></div>
    <div class="dropdown-item"><%= link_to "#{t :link_vr}", locale_root_path(anchor: "vrsection"), class: "page-scroll" %></div>
    <div class="dropdown-item"><%= link_to "#{t :link_tesserart}", locale_root_path(anchor: "tesserartview"), class: "page-scroll" %></div>
    <div class="dropdown-item"><%= link_to "#{t :link_books}", locale_root_path(anchor: "booksview"), class: "page-scroll" %></div>
    <div class="dropdown-divider"></div>
    <div class="dropdown-item"><%= link_to "#{t :link_home}", locale_root_path %></div>
  </div>
</li>

I assume I will need to use Javascript but I'm not sure how to code the solution.我假设我将需要使用 Javascript,但我不确定如何编写解决方案。 I do have jQuery in my Rails application.我的 Rails 应用程序中确实有 jQuery。 Here is code that I use for nav pills to display my tab-content when the tab link is clicked.这是我用于 nav pills 的代码,用于在单击选项卡链接时显示我的选项卡内容。

$(window).load(function() {

  $('#appPills a', '#vrPills a').click(function (e) {
    e.preventDefault()
    $(this).tab('show')
  })

})

I tried both of these but it did not work.我尝试了这两种方法,但没有用。

$(window).load(function() {

  $('.dropdown-item a').click(function (e) {
    $(".dropdown-menu").toggleClass("close");
  })

})

$(window).load(function() {

  $('.dropdown-item').click(function (e) {
    $(".dropdown-menu").toggleClass("close");
  })

})

I remembered that I also use a SmoothScroll script in the dropdown menu.我记得我还在下拉菜单中使用了 SmoothScroll 脚本。 That script is executed for class page-scroll.该脚本是为类页面滚动执行的。 I found the JS code for class page-scroll and added the following code before the page scrolling code.我找到类page-scroll的JS代码,并在页面滚动代码之前添加了以下代码。

$("#navbarDropdown").dropdown('hide')

The dropdown menu closes when the menu item clicks as expected.当菜单项按预期单击时,下拉菜单将关闭。

try this试试这个

    <div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown button
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
  </div>
</div>

Use Default bootstrap dropdown使用默认引导下拉菜单

 <ul class="scrollToSection navbar-nav">
          <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Menu </a>
            <div class="dropdown-menu">
              <div class="col title-block">
                <ul class="scrollToSection navbar-nav">
                  <li class="nav-item dropdown"> <span class="nav-link dropdown-toggle"> Small Plates</span>
                    <div class="dropdown-menu">
 <a class="nav-link dropdown-item active" href="#garden">The Garden</a>
 <a class="nav-link dropdown-item" href="#sea">Sea</a> 
<a class="nav-link dropdown-item" href="#sides">Sides</a>
 <a class="nav-link dropdown-item" href="#farm">Farm</a> </div>
                  </li>
                </ul>
              </div>

            </div>
          </li>
        </ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM