我有多个按钮,当您单击一个按钮时,菜单会下拉,其他所有下拉菜单都将关闭。我目前有9个功能,以及相应的9个功能:
function myFunction5() {
document.getElementById("myDropdown1").classList.remove("show");
document.getElementById("myDropdown2").classList.remove("show");
document.getElementById("myDropdown3").classList.remove("show");
document.getElementById("myDropdown4").classList.remove("show");
document.getElementById("myDropdown5").classList.toggle("show");
document.getElementById("myDropdown6").classList.remove("show");
document.getElementById("myDropdown7").classList.remove("show");
document.getElementById("myDropdown9").classList.remove("show"); }
这将打开下拉菜单5,然后关闭所有其他菜单。
我只想要一个功能而不是9。我该怎么做?
编辑:
完整的代码示例:
<!DOCTYPE html>
<html>
<head>
<style>
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #2980B9;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #ddd;}
.show {display: block;}
</style>
</head>
<body>
<div class="dropdown">
<button onclick="myFunction1()" class="dropbtn">Dropdown</button>
<div id="myDropdown1" class="dropdown-content">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</div>
</div>
<div class="dropdown">
<button onclick="myFunction2()" class="dropbtn">Dropdown</button>
<div id="myDropdown2" class="dropdown-content">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</div>
</div>
<script>
function myFunction1() {
document.getElementById("myDropdown1").classList.toggle("show");
document.getElementById("myDropdown2").classList.remove("show");
}
function myFunction2() {
document.getElementById("myDropdown1").classList.remove("show");
document.getElementById("myDropdown2").classList.toggle("show");
}
</script>
</body>
</html>
想象一下9次...
答案 0 :(得分:0)
您是否尝试过创建一个包含所有菜单的节点列表,然后将要打开的菜单作为参数传递给一个函数,该函数然后遍历您的节点列表并关闭除传递给它的菜单之外的每个菜单? >
答案 1 :(得分:0)
尝试一下:-
<script type="text/javascript">
function manageDropdown(id)
{
var dropdown = document.getElementsByClassName('dropdown-content');
for(i=0;i<dropdown.length;i++)
{
var innerId = dropdown[i].getAttribute('id');
if (innerId == id) {
dropdown[i].classList.remove("hide");
} else {
dropdown[i].classList.add("hide");
}
}
}
</script>
<div class="dropdown">
<button onclick="manageDropdown('myDropdown1')" class="dropbtn">Dropdown</button>
<div id="myDropdown1" class="dropdown-content">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</div>
</div>
<div class="dropdown">
<button onclick="manageDropdown('myDropdown2')" class="dropbtn">Dropdown</button>
<div id="myDropdown2" class="dropdown-content hide">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</div>
</div>