gpt4 book ai didi

jquery - 多级下拉 Bootstrap

转载 作者:太空宇宙 更新时间:2023-11-04 01:26:40 24 4
gpt4 key购买 nike

我已经试过了 Multi-Level Drop Down .当我有 2 个或更多下拉子菜单时。当我单击第一个下拉菜单然后单击另一个下拉菜单时,第一个下拉菜单仍然打开。我希望当我点击第二个下拉菜单时,第一个下拉菜单会关闭,或者当我点击下拉菜单的头部时,所有子下拉菜单都会关闭。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.dropdown-submenu {
position: relative;
}

.dropdown-submenu .dropdown-menu {
top: 0;
left: 100%;
margin-top: -1px;
}
</style>
</head>
<body>

<div class="container">
<h2>Multi-Level Dropdowns</h2>
<p>In this example, we have created a .dropdown-submenu class for multi-level dropdowns (see style section above).</p>
<p>Note that we have added jQuery to open the multi-level dropdown on click (see script section below).</p>
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Tutorials
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a tabindex="-1" href="#">HTML</a></li>
<li><a tabindex="-1" href="#">CSS</a></li>
<li class="dropdown-submenu">
<a class="test" tabindex="-1" href="#">New dropdown 1 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a tabindex="-1" href="#">2nd level dropdown</a></li>
<li><a tabindex="-1" href="#">2nd level dropdown</a></li>
<li class="dropdown-submenu">
<a class="test" href="#">Another dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">3rd level dropdown</a></li>
<li><a href="#">3rd level dropdown</a></li>
</ul>
</li>
</ul>
</li>
<li class="dropdown-submenu">
<a class="test" tabindex="-1" href="#">New dropdown 2 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a tabindex="-1" href="#">2nd level dropdown</a></li>
<li><a tabindex="-1" href="#">2nd level dropdown</a></li>
<li class="dropdown-submenu">
<a class="test" href="#">Another dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">3rd level dropdown</a></li>
<li><a href="#">3rd level dropdown</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>

<script>
$(document).ready(function(){
$('.dropdown-submenu a.test').on("click", function(e){
$(this).next('ul').toggle();
e.stopPropagation();
e.preventDefault();
});
});
</script>

</body>
</html>

enter image description here

最佳答案

您可以使用 CSS、jQuery addClass('active') & toggleClass('active') 在 CSS 中执行此操作,您必须制作 display: none ; 所有下拉菜单

给你

$(document).ready(function(){
$('.dropdown-submenu a.test').on("click", function(e){
$(this).next('ul').toggle();
e.stopPropagation();
e.preventDefault();
});
});

$(function () {
$('#drop1').on('click', function(){
$('.dropmenu1').toggleClass('active');
$('.dropmenu2').removeClass('active');
});

$('#drop2').on('click', function(){
$('.dropmenu1').removeClass('active');
$('.dropmenu2').toggleClass('active');
});

$('#drop3').on('click', function(){
$('.dropmenu3').toggleClass('active');
});

$('#drop4').on('click', function(){
$('.dropmenu4').toggleClass('active');
});
$('#button-t').on('click', function(){
$('.dropmenu1').removeClass('active');
$('.dropmenu2').removeClass('active');
$('.dropmenu3').removeClass('active');
$('.dropmenu4').removeClass('active');
});
});
.dropdown-submenu {
position: relative;
}

.dropdown-submenu .dropdown-menu {
top: 0;
left: 100%;
margin-top: -1px;
}
ul.dropdown-menu ul.dropdown-menu{
display:none !important;
}
ul.dropdown-menu ul.dropdown-menu.active{
display:block !important;
}
ul.dropdown-menu ul
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="container">
<h2>Multi-Level Dropdowns</h2>
<p>In this example, we have created a .dropdown-submenu class for multi-level dropdowns (see style section above).</p>
<p>Note that we have added jQuery to open the multi-level dropdown on click (see script section below).</p>
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" id="button-t">Tutorials
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a tabindex="-1" href="#">HTML</a></li>
<li><a tabindex="-1" href="#">CSS</a></li>
<li class="dropdown-submenu">
<a class="test" tabindex="-1" href="#" id="drop1"> New dropdown 1 <span class="caret"></span></a>
<ul class="dropdown-menu dropmenu1">
<li><a tabindex="-1" href="#">2nd level dropdown</a></li>
<li><a tabindex="-1" href="#">2nd level dropdown</a></li>
<li class="dropdown-submenu">
<a class="test" href="#" id="drop3">Another dropdown <span class="caret"></span></a>
<ul class="dropdown-menu dropmenu3">
<li><a href="#">3rd level dropdown</a></li>
<li><a href="#">3rd level dropdown</a></li>
</ul>
</li>
</ul>
</li>
<li class="dropdown-submenu" >
<a class="test" tabindex="-1" href="#" id="drop2">New dropdown 2 <span class="caret"></span></a>
<ul class="dropdown-menu dropmenu2">
<li><a tabindex="-1" href="#">2nd level dropdown</a></li>
<li><a tabindex="-1" href="#">2nd level dropdown</a></li>
<li class="dropdown-submenu">
<a class="test" href="#" id="drop4">Another dropdown <span class="caret"></span></a>
<ul class="dropdown-menu dropmenu4">
<li><a href="#">3rd level dropdown</a></li>
<li><a href="#">3rd level dropdown</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>

关于jquery - 多级下拉 Bootstrap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47937073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com