Bootstrap 4嵌套下拉菜单?
Posted
技术标签:
【中文标题】Bootstrap 4嵌套下拉菜单?【英文标题】:Bootstrap 4 nested dropdown? 【发布时间】:2019-01-23 02:38:59 【问题描述】:我想要做的是,如果有人点击艺术,那么历史社会学和地理子菜单应该会出现。现在应该会在有人点击 Arts 时出现。https://jsfiddle.net/0e7jdfza/
我尝试添加此代码 https://jsfiddle.net/w3zyLmgb/ 来代替艺术 li 和 ul,但它在下拉菜单中工作,尽管它在此下拉菜单之外工作
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<div class="dropdown show">
<a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Select Streams
</a>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<li><a class="dropdown-item" href="#">Science</a></li>
<li><a class="dropdown-item" href="#">Commerce</a></li>
<li class="nav-item dropdown"><a class="dropdown-item" href="#">Arts</a>
<ul>
<li>History</li>
<li>Sociology</li>
<li>Geogrphy</li>
</ul>
</li>
</ul>
</div>
<!-- Optional javascript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
【问题讨论】:
【参考方案1】:我认为this 有点像您正在寻找的东西。
您的更新代码与其中给出的建议之一看起来与给出的示例类似。
(如果你使用 Angular,你可以使用 ngx-dropdowns 他们有嵌套下拉菜单的解决方案)
$('.dropdown-menu a.dropdown-toggle').on('click', function(e)
if (!$(this).next().hasClass('show'))
$(this).parents('.dropdown-menu').first().find('.show').removeClass("show");
var $subMenu = $(this).next(".dropdown-menu");
$subMenu.toggleClass('show');
$(this).parents('li.nav-item.dropdown.show').on('hidden.bs.dropdown', function(e)
$('.dropdown-submenu .show').removeClass("show");
);
return false;
);
.dropdown-submenu
position: relative;
.dropdown-submenu a::after
transform: rotate(-90deg);
position: absolute;
right: 6px;
top: .8em;
.dropdown-submenu .dropdown-menu
top: 0;
left: 100%;
margin-left: .1rem;
margin-right: .1rem;
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<div class="dropdown show">
<a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Select Streams
</a>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<li><a class="dropdown-item" href="#">Science</a></li>
<li><a class="dropdown-item" href="#">Commerce</a></li>
<li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" href="#">Arts</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">History</a></li>
<li><a class="dropdown-item" href="#">Geography</a></li>
<li><a class="dropdown-item" href="#">Sociology</a></li>
</ul>
</li>
</ul>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
【讨论】:
到dropleft,将css代码中的left:100%
修改为right:100%
【参考方案2】:
在下面的代码中,我使用 Django 模板 for loop
循环多个下拉数据。此代码生成 2 级下拉菜单,左侧第二级下拉:
<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">
Dropdown
</a>
<ul class="dropdown-menu " aria-labelledby="navbarDropdown">
% for campus in object_list %
<li class="dropdown-submenu dropleft">
<a class="dropdown-item dropdown-toggle " href="#"> campus.name </a>
<ul class="dropdown-menu ">
% for school in campus.school.all %
<li><a class="dropdown-item"
href=""> school.name </a>
</li>
% endfor %
</ul>
</li>
% endfor %
</ul>
</li>
【讨论】:
【参考方案3】:$(document).ready(function ()
$("#type").change(function ()
var val = $(this).val();
if (val == "arts")
$("#type").html("<option value='history'>history</option><option value='sociology'>sociology</option><option value='geography'>geography</option>");
else if (val == "science")
$("#type").html("<option value='maths'>maths</option><option value='physics'>physics</option>");
);
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="type" class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<option value="na">--Select an Item--</option>
<option value="arts">Arts</option>
<option value="science">Science</option>
</select>
【讨论】:
以上是关于Bootstrap 4嵌套下拉菜单?的主要内容,如果未能解决你的问题,请参考以下文章