gpt4 book ai didi

javascript - 多级 Bootstrap 菜单只显示第一级

转载 作者:太空宇宙 更新时间:2023-11-03 20:30:36 25 4
gpt4 key购买 nike

我正在尝试使用 bootstrap 3 创建一个多级菜单,但无法让它工作,而且我对 bootstrap 的了解不足,无法找出原因

标记是

<div class="dropdown">
<a role="button" data-toggle="dropdown" class="btn btn-primary" data-target="" href="">Y<span class="caret"></span></a>
<ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
<li class="dropdown-submenu">
<a href="#" tabindex="-1">foo</a>
<ul class="dropdown-menu">
<li>
<a href="">bar</a>
</li>
</ul>
</li>
</ul>
</div>

css和js包含如下

<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">

<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>

我的期望是,当我打开第一个菜单(“Y”)时,我可以看到一个新的菜单项“foo”,打开它可以显示最后一个元素“bar”

该代码基于我找到的示例 here

最佳答案

似乎该片段中的 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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="dropdown">
<a role="button" data-toggle="dropdown" class="btn btn-primary" data-target="" href="">Y<span class="caret"></span></a>
<ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
<li class="dropdown-submenu">
<a href="#" tabindex="-1">foo</a>
<ul class="dropdown-menu">
<li>
<a href="">bar</a>
</li>
</ul>
</li>
</ul>
</div>

关于javascript - 多级 Bootstrap 菜单只显示第一级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42923080/

25 4 0