我想做的是,如果有人单击艺术,则应该显示“历史社会学和地理”子菜单。现在应该会在有人点击“艺术”时显示。
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>
答案 0 :(得分:0)
$(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>
答案 1 :(得分:0)
您的更新后的代码具有其中给出的建议之一,看起来像给出的示例。
(如果您使用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>
答案 2 :(得分:0)
在以下代码中,我使用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>