我不熟悉JavaScript,我希望得到一个我似乎无法解决的问题一点帮助。我目前在我的网站上有2个下拉菜单。一个是点击汉堡菜单图标时激活的导航下拉菜单。第二个下拉菜单用于在我的网站上显示类别。目前,当我点击一个下拉菜单时,我必须再次点击才能关闭它。如果我点击第二个下拉菜单而没有关闭第一个下拉菜单,它们仍然可见。我想要发生的是两件事。首先,我想这样做,以便如果用户点击下拉菜单中div的任何位置,它会自动关闭。我希望看到的第二件事是每次只能看到一个下拉菜单。所以,如果我点击一个,另一个下拉打开,我希望它被关闭。希望我解释得很好。现在到我正在使用的代码。如何关闭一个Javascript下拉菜单时打开另一个
以下是在我的脑海中。
<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function DropDownMenuNavigation() {
document.getElementById("b2DropDownMenuNav").classList.toggle("show");
}
function DropDownMenuCategory() {
document.getElementById("b2DropDownMenuCat").classList.toggle("show");
}
</script>
然后我使用这个按钮来激活导航下拉菜单。这包含在我的身体内。
<div class="dropbtn" style="float: left;">
<button onclick="DropDownMenuNavigation()" class="dropbtn">☰ MENU</button>
</div>
这是我用来包括我的类别下拉菜单。
<div class="dropbtn" style="float: left;">
<button onclick="DropDownMenuCategory()" class="dropbtn">CATEGORIES</button>
</div>
现在最后是我使用的CSS只是在帮助任何的机会。
/* Dropdown Button */
.dropbtn {
background-color: #0066a2;
color: white;
padding: 1px;
font-size: 15px;
font-weight: bold;
border: none;
cursor: pointer;
}
.dropbtn a {
color: #FFFFFF;
text-decoration: none;
font-size: 15px;
font-weight: bold;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
float: left;
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #0066a2;
min-width: 260px;
max-width: 960px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
color: #000000;
text-decoration: none;
}
/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}
那么最好的方法去做什么我问?有人能帮我一把,指出我的方向吗?非常感谢,我感谢您可以借我的任何帮助。
所以选择另一个和删除类 – epascarello
是的,我想这样做,但我有尝试一切似乎并没有制定出适合我。我会如何去做这件事?我所要做的就是从对面的下拉菜单中删除类“show”? – Born2DoubleUp
所以你做了:'document.getElementById(“b2DropDownMenuNav”)。classList。删除(“显示”);' – epascarello