当前位置: 首页 > 知识库问答 >
问题:

最新版本引导中的工作下拉菜单?[副本]

隆芷阳
2023-03-14

我正在开发一个网站,需要一个下拉菜单内的下拉菜单,但显然在Bootstrap5.0中,他们删除了该功能?我听说仍然有办法获得子菜单,但我尝试过的所有方法都不起作用,请帮助!

共有2个答案

松亦
2023-03-14

下面是获取菜单下的子菜单的代码。

HTML:

>  <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
>     <div class="container-fluid">
>     <a class="navbar-brand" href="#">Brand</a>
>     <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#main_nav" 
> aria-expanded="false" aria-label="Toggle navigation">
>     <span class="navbar-toggler-icon"></span>
>     </button>
>     <div class="collapse navbar-collapse" id="main_nav">
>       <ul class="navbar-nav">
>         <li class="nav-item active"> <a class="nav-link" href="#">Home </a> </li>
>         <li class="nav-item"><a class="nav-link" href="#"> About </a></li>
>         <li class="nav-item dropdown" id="myDropdown">
>           <a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">  Treeview menu  </a>
>           <ul class="dropdown-menu">
>             <li> <a class="dropdown-item" href="#"> Dropdown item 1 </a></li>
>             <li> <a class="dropdown-item" href="#"> Dropdown item 2 &raquo; </a>
>               <ul class="submenu dropdown-menu">
>                 <li><a class="dropdown-item" href="#">Submenu item 1</a></li>
>                 <li><a class="dropdown-item" href="#">Submenu item 2</a></li>
>                 <li><a class="dropdown-item" href="#">Submenu item 3 &raquo; </a>
>                   <ul class="submenu dropdown-menu">
>                     <li><a class="dropdown-item" href="#">Multi level 1</a></li>
>                     <li><a class="dropdown-item" href="#">Multi level 2</a></li>
>                   </ul>
>                 </li>
>                 <li><a class="dropdown-item" href="#">Submenu item 4</a></li>
>                 <li><a class="dropdown-item" href="#">Submenu item 5</a></li>
>               </ul>
>             </li>
>             <li><a class="dropdown-item" href="#"> Dropdown item 3 </a></li>
>             <li><a class="dropdown-item" href="#"> Dropdown item 4 </a></li>
>           </ul>
>         </li>
>       </ul>
>     </div>
>     <!-- navbar-collapse.// -->
>     </div>
>     <!-- container-fluid.// -->
>     </nav>

JavaScript:

document.addEventListener("DOMContentLoaded", function(){
// make it as accordion for smaller screens
if (window.innerWidth < 992) {

  // close all inner dropdowns when parent is closed
  document.querySelectorAll('.navbar .dropdown').forEach(function(everydropdown){
    everydropdown.addEventListener('hidden.bs.dropdown', function () {
      // after dropdown is hidden, then find all submenus
        this.querySelectorAll('.submenu').forEach(function(everysubmenu){
          // hide every submenu as well
          everysubmenu.style.display = 'none';
        });
    })
  });

  document.querySelectorAll('.dropdown-menu a').forEach(function(element){
    element.addEventListener('click', function (e) {
        let nextEl = this.nextElementSibling;
        if(nextEl && nextEl.classList.contains('submenu')) {    
          // prevent opening link if link needs to open dropdown
          e.preventDefault();
          if(nextEl.style.display == 'block'){
            nextEl.style.display = 'none';
          } else {
            nextEl.style.display = 'block';
          }

        }
    });
  })
}
// end if innerWidth
}); 
// DOMContentLoaded  end

响应CSS:

/* ============ desktop view ============ */
@media all and (min-width: 992px) {
    .dropdown-menu li{ position: relative;  }
    .nav-item .submenu{ 
        display: none;
        position: absolute;
        left:100%; top:-7px;
    }
    .nav-item .submenu-left{ 
        right:100%; left:auto;
    }
    .dropdown-menu > li:hover{ background-color: #f1f1f1 }
    .dropdown-menu > li:hover > .submenu{ display: block; }
}   
/* ============ desktop view .end// ============ */

/* ============ small devices ============ */
@media (max-width: 991px) {
  .dropdown-menu .dropdown-menu{
      margin-left:0.7rem; margin-right:0.7rem; margin-bottom: .5rem;
  }
}   
/* ============ small devices .end// ============ */
谯和煦
2023-03-14

这是实现这一点的干净方法!

(function($bs) {
        const CLASS_NAME = 'has-child-dropdown-show';
    $bs.Dropdown.prototype.toggle = function(_orginal) {
        return function() {
            document.querySelectorAll('.' + CLASS_NAME).forEach(function(e) {
                e.classList.remove(CLASS_NAME);
            });
            let dd = this._element.closest('.dropdown').parentNode.closest('.dropdown');
            for (; dd && dd !== document; dd = dd.parentNode.closest('.dropdown')) {
                dd.classList.add(CLASS_NAME);
            }
            return _orginal.call(this);
        }
    }($bs.Dropdown.prototype.toggle);

    document.querySelectorAll('.dropdown').forEach(function(dd) {
        dd.addEventListener('hide.bs.dropdown', function(e) {
            if (this.classList.contains(CLASS_NAME)) {
                this.classList.remove(CLASS_NAME);
                e.preventDefault();
            }
            if(e.clickEvent && e.clickEvent.composedPath().some(el=>el.classList && el.classList.contains('dropdown-toggle'))){
                e.preventDefault();
            }
            e.stopPropagation(); // do not need pop in multi level mode
        });
    });

    // for hover
    function getDropdown(element) {
        return $bs.Dropdown.getInstance(element) || new $bs.Dropdown(element);
    }

    document.querySelectorAll('.dropdown-hover, .dropdown-hover-all .dropdown').forEach(function(dd) {
        dd.addEventListener('mouseenter', function(e) {
            let toggle = e.target.querySelector(':scope>[data-bs-toggle="dropdown"]');
            if (!toggle.classList.contains('show')) {
                getDropdown(toggle).toggle();
            }
        });
        dd.addEventListener('mouseleave', function(e) {
            let toggle = e.target.querySelector(':scope>[data-bs-toggle="dropdown"]');
            if (toggle.classList.contains('show')) {
                getDropdown(toggle).toggle();
            }
        });
    });
})(bootstrap);
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, shrink-to-fit=no" />
    <!-- Begin SEO tag -->
    <title>Infinite Multiple Level Dropdown Menu base on Bootstrap</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
    <style>.dropdown-hover-all .dropdown-menu, .dropdown-hover > .dropdown-menu.dropend { margin-left:-1px !important }</style>
</head>

<body class="">
    <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
        <div class="collapse navbar-collapse" id="navbarsExampleDefault">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item active">
                    <a class="nav-link" href="#">Demo <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item dropdown dropdown-hover">
                    <a class="nav-link dropdown-toggle" href="#" id="dropdown01" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Hover</a>
                    <div class="dropdown-menu" aria-labelledby="dropdown01">
                        <a class="dropdown-item" href="#">Single App</a>
                        <a class="dropdown-item" href="#">Multiple Works</a>
                        <div class="dropdown-divider"></div>
                        <a class="dropdown-item" href="#">Disctribution</a>
                    </div>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="dropdown02" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documents</a>
                    <div class="dropdown-menu" aria-labelledby="dropdown02">
                        <a class="dropdown-item" href="#">Introduction</a>
                        <div class="dropdown dropend">
                            <a class="dropdown-item dropdown-toggle" href="#" id="dropdown-layouts" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Layouts</a>
                            <div class="dropdown-menu" aria-labelledby="dropdown-layouts">
                                <a class="dropdown-item" href="#">Basic</a>
                                <a class="dropdown-item" href="#">Compact Aside</a>
                                <div class="dropdown-divider"></div>
                                <div class="dropdown dropend">
                                    <a class="dropdown-item dropdown-toggle" href="#" id="dropdown-layouts" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Custom</a>
                                    <div class="dropdown-menu" aria-labelledby="dropdown-layouts">
                                        <a class="dropdown-item" href="#">Fullscreen</a>
                                        <a class="dropdown-item" href="#">Empty</a>
                                        <div class="dropdown-divider"></div>
                                        <a class="dropdown-item" href="#">Magic</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="dropdown-divider"></div>
                        <a class="dropdown-item" href="#">Something else here</a>
                    </div>
                </li>
                <li class="nav-item active">
                    <a class="nav-link" href="https://github.com/dallaslu/bootstrap-5-multi-level-dropdown">View on Github</a>
                </li>
            </ul>
        </div>
    </nav>
    <div class="container" style="padding-top: 3.5rem">
        <div class="dropdown mt-3">
            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-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>
                <div class="dropdown dropend">
                    <a class="dropdown-item dropdown-toggle" href="#" id="dropdown-layouts" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Layouts</a>
                    <div class="dropdown-menu" aria-labelledby="dropdown-layouts">
                        <a class="dropdown-item" href="#">Basic</a>
                        <a class="dropdown-item" href="#">Compact Aside</a>
                        <div class="dropdown-divider"></div>
                        <div class="dropdown dropend">
                            <a class="dropdown-item dropdown-toggle" href="#" id="dropdown-layouts" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Custom</a>
                            <div class="dropdown-menu" aria-labelledby="dropdown-layouts">
                                <a class="dropdown-item" href="#">Fullscreen</a>
                                <a class="dropdown-item" href="#">Empty</a>
                                <div class="dropdown-divider"></div>
                                <a class="dropdown-item" href="#">Magic</a>
                            </div>
                        </div>
                    </div>
                </div>
                <a class="dropdown-item" href="#">Something else here</a>
            </div>
        </div>
        <div class="d-flex dropdown-hover-all">
          <div class="dropdown mt-3">
              <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton222" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  Dropdown Hover
              </button>
              <div class="dropdown-menu" aria-labelledby="dropdownMenuButton222">
                  <a class="dropdown-item" href="#">Action</a>
                  <div class="dropdown dropend">
                      <a class="dropdown-item dropdown-toggle" href="#" id="dropdown-layouts" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Layouts</a>
                      <div class="dropdown-menu" aria-labelledby="dropdown-layouts">
                          <a class="dropdown-item" href="#">Basic</a>
                          <a class="dropdown-item" href="#">Compact Aside</a>
                          <div class="dropdown-divider"></div>
                          <div class="dropdown dropend">
                              <a class="dropdown-item dropdown-toggle" href="#" id="dropdown-layouts" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Custom</a>
                              <div class="dropdown-menu" aria-labelledby="dropdown-layouts">
                                  <a class="dropdown-item" href="#">Fullscreen</a>
                                  <a class="dropdown-item" href="#">Empty</a>
                                  <div class="dropdown-divider"></div>
                                  <a class="dropdown-item" href="#">Magic</a>
                              </div>
                          </div>
                      </div>
                  </div>
                  <a class="dropdown-item" href="#">Something else here</a>
              </div>
            </div>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.1/dist/umd/popper.min.js" integrity="sha384-SR1sx49pcuLnqZUnnPwx6FCym0wLsk5JZuNx2bPPENzswTNFaQU1RDvt3wT4gWFG" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.min.js" integrity="sha384-j0CNLUeiqtyaRmlzUHCPZ+Gy5fQu0dQ6eZ/xAww941Ai1SxSY+0EQqNXNE6DZiVc" crossorigin="anonymous"></script>

</body>

</html>
 类似资料:
  • 嗨,我正在尝试自动启动下拉菜单。默认情况下,它的可见性是隐藏的。将鼠标悬停在它上面时,它的可见性属性显示为可见。我可以单击下拉菜单,但是在单击下拉菜单后,我的selenium脚本不能从下拉菜单中选择值。 错误:线程"main"org.openqa.selenium.ElementNotVisibleException异常:无法单击元素 HTML代码段

  • Bootstrap3仍在RC中,但我只是尝试实现它。我不知道如何设置子菜单类。即使css中没有类,即使是新文档也没有提到它 它存在于2. x中,类名为下拉子菜单

  • 我正在建立一个自定义的网站使用一个儿童主题的Wordpress‘店面',但我有很大的问题与移动引导导航切换。 与菜单切换响应菜单,它不显示下拉在870px宽,只显示在714px宽。我一直试图解决这个问题,但没有成功,我唯一注意到的是切换器上的按钮类。如果您查看代码检查器,aria-expanded=“false”显示在大约870px宽的地方,如果我手动在代码中输入值“true”,那么它会读为ari

  • 引用example_NSD_DropList.nsi的内容: !include "MUI2.nsh" Name "example_NSD_DropList" OutFile "example_NSD_DropList.exe" InstallDir "$EXEDIR" Page custom nsDialogsPage !insertmacro MUI_LANGUA

  • 关于我的上一个问题(bootstrap上的下拉菜单不起作用),我正在为li下拉菜单的子菜单构建一个子菜单。这意味着它是嵌套的。银行菜单下有交易下拉菜单,交易下拉菜单下有不同类型交易的菜单。顺便说一下,我已经成功地创建了它,但是在悬停期间,“事务”模块的子菜单出现在事务菜单的前面,阻止了作为事务的父菜单。在悬停期间,如何避免它并将其放置在事务菜单的右侧?不管怎样,我已经使用了“右下拉菜单”,但它不会

  • 在Bootstrap 4中最简单的方法是什么?我在SO上找到的所有示例要么太乱,要么没有包含在NAV中。 我试过把下拉列表放在下拉列表中,但这似乎不起作用。有人能帮我弄一下这个吗? 下面是我的代码的基本大纲: null null