如何在Bootstrap 5中的下拉菜单项中添加子菜单?
这是我使用Bootstrap及其所有下拉台创建此导航栏的代码,但我想要要添加另一个下拉列表,将网络开发内部的服务下拉部分添加到下拉部分,但对于Bootstrap5进行操作并不简单的解决方案...
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Services
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<!-- webDevelopment DropDown -->
<li id="menu-item-1644" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-1644"><a href="http://localhost:8888/wordpress-experiments/level-1/level-2/">Web Development</a>
<ul class="sub-menu">
<li id="menu-item-1645" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-1645"><a href="http://localhost:8888/wordpress-experiments/level-1/level-2/level-3/">Level 3</a></li>
<li id="menu-item-1699" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-1699"><a href="http://localhost:8888/wordpress-experiments/level-1/level-2/level-3a/">Level 3a</a></li>
<li id="menu-item-1700" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-1700"><a href="http://localhost:8888/wordpress-experiments/level-1/level-2/level-3b/">Level 3b</a></li>
</ul>
</li>
<li><a class="dropdown-item" href="#">Software Development</a></li>
<li><a class="dropdown-item" href="#">Networking and Cloud</a></li>
<li><a class="dropdown-item" href="#">Cyber Defence and offensive</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">More</a></li>
</ul>
</li>
this is my code which i have created this navigation bar with bootstrap and all of its drop downs but i want to add another drop down to the services drop down section inside of webdevelopment but it can't any easy solution for this to done with bootstrap5...
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Services
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<!-- webDevelopment DropDown -->
<li id="menu-item-1644" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-1644"><a href="http://localhost:8888/wordpress-experiments/level-1/level-2/">Web Development</a>
<ul class="sub-menu">
<li id="menu-item-1645" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-1645"><a href="http://localhost:8888/wordpress-experiments/level-1/level-2/level-3/">Level 3</a></li>
<li id="menu-item-1699" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-1699"><a href="http://localhost:8888/wordpress-experiments/level-1/level-2/level-3a/">Level 3a</a></li>
<li id="menu-item-1700" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-1700"><a href="http://localhost:8888/wordpress-experiments/level-1/level-2/level-3b/">Level 3b</a></li>
</ul>
</li>
<li><a class="dropdown-item" href="#">Software Development</a></li>
<li><a class="dropdown-item" href="#">Networking and Cloud</a></li>
<li><a class="dropdown-item" href="#">Cyber Defence and offensive</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">More</a></li>
</ul>
</li>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您希望“更多”链接具有子菜单。只需将另一个菜单放入列表项目中:
按照您喜欢的样式,
并使用CSS使其无形且绝对:
悬停在链接时可见:
I assume you want the "more" link to have a submenu. Just put in another menu in to the list item:
Style it however you like,
and use CSS to make it invisible and absolute:
And when the link is hovered make it visible:
因此,这是我发现的答案,但请注意,仅带Bootstrap 5
。
so this is the answer i found but note that this is only available with bootstrap 5.
CSS:
您仍然可以使用bootstrap来定位子菜单,而不是覆盖定位样式。只需在链接上使用data-bs-toggle =“下拉”,就像往常一样触发子菜单。您必须在单击处理程序上添加一些JS来调用stopPropagation(),以便链接触发子菜单以阻止菜单在单击时立即消失。
在父上使用“ Dropend”将子菜单放置在链接的右侧。
还要使用data-bs-display =“ static”来禁用动态popper.js定位。这是因为子菜单与主菜单绝对放置,并且popper.js将计算出该父母内部的子菜单没有足够的空间,这会导致其不正确地重新定位子菜单。
HTML摘要
JS
You can still use bootstrap to position the submenu rather than overriding the positioning styles. Just use data-bs-toggle="dropdown" on the link triggering the submenu like usual. You have to add a bit of JS to call stopPropagation() on the click handler for the link triggering the submenu to stop the menus from immediately disppearing when clicked.
Use "dropend" on the parent to position the submenu to the right of the link.
Also use data-bs-display="static" to disable dynamic popper.js positioning. This is because the submenu is positioned absolutely relative to the main menu and popper.js will calculate that there is not enough space for the submenu within this parent, which will cause it to reposition the submenu incorrectly.
HTML snippet
JS