我有一个顶级下拉菜单内的多个下拉菜单。我有一个问题,当我点击打开一个子菜单,它仍然打开,当我点击打开另一个。想自动关闭其他子菜单时,另一个点击打开。
我的HTML:
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<div class="container-fluid"> <a class="navbar-brand" href="#"> LOGO </a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button>
<!-- start test -->
<div class="row navbar-collapse" id="navbarCollapse">
<div class="col-md-12 order-last menu-one">
<!-- start-->
<ul class="navbar-nav ms-auto mb-2 mb-md-0 nav navbar-right justify-content-end">
<hr class="white d-block d-md-none">
<li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">login </a> </li>
<li class="nav-item"> <a class="nav-link" href="#">support <span class="text-ltred">1300 000 123</span></a> </li>
<li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Global sites </a>
<ul class="dropdown-menu dropdown-menu-end">
<li><a class="dropdown-item" href="#">United States / Canada</a></li>
<li><a class="dropdown-item" href="#">New Zealand</a></li>
</ul>
</li>
<li class="nav-item" id="search">
<div class="search-box nav-link">
<input type="text" class="search-input" placeholder="Search">
<a class="search-btn" href="#"> ? </a> </div>
</li>
</ul>
<!-- end -->
</div>
<div class="col-md-12 order-md-last menu-two">
<!-- start-->
<ul class="navbar-nav ms-auto mb-2 mb-md-0 nav navbar-right justify-content-end">
<li class="nav-item dropdown has-megamenu"> <a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">Solutions & services</a>
<div class="dropdown-menu megamenu" role="menu">
<div class="row g-3">
<div class="col-md-6 col-12">
<div class="col-megamenu">
<h6 class="title">Solutions & services</h6>
<ul class="list-unstyled">
<li class="dropend"> <a href="#" class="dropdown-item dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" data-bs-auto-close="true" aria-expanded="false">Leasing solutions</a>
<ul class="dropdown-menu no-border">
<li><a class="dropdown-item" href=""> Transfer my fleet</a></li>
<li><a class="dropdown-item" href=""> Start a new fleet</a></li>
<li><a class="dropdown-item" href=""> Electric vehicles</a></li>
<li><a class="dropdown-item" href=""> Next lease</a></li>
<li><a class="dropdown-item" href=""> Trucks, plant & equipment</a></li>
<li><a class="dropdown-item" href=""> Used vehicles</a></li>
</ul>
</li>
<li class="dropend"> <a href="#" class="dropdown-item dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" data-bs-auto-close="true" aria-expanded="false">Fleet management services</a>
<ul class="dropdown-menu no-border">
<li><a class="dropdown-item" href=""> Accident management</a></li>
<li><a class="dropdown-item" href=""> Acquisition and disposals</a></li>
<li><a class="dropdown-item" href=""> Driver safety</a></li>
<li><a class="dropdown-item" href=""> Fleet card</a></li>
<li><a class="dropdown-item" href=""> Hire vehicles</a></li>
<li><a class="dropdown-item" href=""> Infringement management</a></li>
<li><a class="dropdown-item" href=""> Insurance</a></li>
<li><a class="dropdown-item" href=""> Maintenance</a></li>
<li><a class="dropdown-item" href=""> PoolCar sharing platform</a></li>
<li><a class="dropdown-item" href=""> Registration</a></li>
<li><a class="dropdown-item" href=""> Roadside assistance</a></li>
<li><a class="dropdown-item" href=""> Telematics</a></li>
<li><a class="dropdown-item" href=""> Toll management</a></li>
</ul>
</li>
<li class="dropend"> <a href="#" class="dropdown-item dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" data-bs-auto-close="true" aria-expanded="false">Fleet optimisation</a>
<ul class="dropdown-menu no-border">
<li><a class="dropdown-item" href=""> Corporate financing</a></li>
<li><a class="dropdown-item" href=""> Leasing</a></li>
<li><a class="dropdown-item" href=""> Sales and lease back</a></li>
<li><a class="dropdown-item" href=""> Strategic consulting</a></li>
<li><a class="dropdown-item" href=""> Technology integration</a></li>
</ul>
</li>
</ul>
</div>
<!-- col-megamenu.// -->
</div>
<!-- end col-3 -->
</div>
<!-- end row -->
</div>
<!-- dropdown-mega-menu.// -->
</li>
<li class="nav-item"> <a class="nav-link" href="#">Driver Support</a></li>
<li class="nav-item"> <a class="nav-link" href="#">About</a></li>
<li class="nav-item"> <a class="nav-link" href="#">News</a></li>
<li class="nav-item"> <a class="nav-link" href="#">Resources</a></li>
<li class="nav-item"> <a class="btn btn-bd-primary" href="#" role="button">Contact us</a></li>
</ul>
<!-- end -->
</div>
</div>
<!-- end test -->
</div>
</nav>
你可以看到我的完整代码:https://www.codeply.com/p/YfWQ2HX73q
我被卡住了,真的很想得到帮助,让这个菜单工作得很漂亮。
2条答案
按热度按时间wnavrhmk1#
将所有子菜单放入列表中,然后添加单击侦听器,单击时,关闭列表中的所有子菜单,但单击的子菜单除外:
工作片段:
fgw7neuy2#
使用
for
循环遍历带有className=dropdown-menu
的元素,并相应地更改它className
例如: