gpt4 book ai didi

javascript - 如何正确处理多个下拉按钮

转载 作者:太空宇宙 更新时间:2023-11-04 02:39:43 25 4
gpt4 key购买 nike

我正在做的这个系统的问题是我似乎无法让下拉按钮相互配合。我只想在单击另一个按钮时关闭第一个单击的按钮。到目前为止,除非再次单击或在下拉区域外单击,否则它们只是相互堆叠。

这是我的代码:

<html>
<style>
.dropbtn {
background-color: #00BFFF;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
background-color: #1E90FF;
}

.dropdown2 {
position: relative;
display: block;
}


.dropdown-content2 {
display: none;
position: absolute;
background-color: #1E90FF;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content2 a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;

}

.dropdown2 a:hover {
background-color: #00BFFF;

}

.dropdown {
position: relative;
display: block;
}


.dropdown-content {
display: none;
position: absolute;
background-color: #1E90FF;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;

}

.dropdown a:hover {
background-color: #00BFFF;

}

.show {
display:block;
position: relative;
left:18.6%;
top:-20%;
width: 80%;
}

.border2 {
float: left;
border: 2px #FA5858;
background-color: #FFFFFF;
margin: 0 auto;
padding-bottom:40px;
padding-top:5px;
width : 86%;
height : 44%;
top : -10%;
left : 7%;
position : relative;
padding: 1em;
}
</style>


<body>
<div class = "border2">
<div style = "font-size:25px;list-style-type: none;margin: 0;padding: 10px;overflow: hidden;background-color: #BDBDBD;
position : relative;top : -10%;width : 101.2%;left:-1.5%;"><strong>NEED HELP?</strong></div>

<div class="dropdown">

<button onclick="myFunction()" class="dropbtn" style = "position:absolute; left:-1.5%; width:20%;">GUIDES</button>
<div id="myDropdown" class="dropdown-content">
<ul>
<li><a>asasd.</a></li>
<li><a>Thasdat.</a></li>
<li><a>test2.</a></li>
</ul>
</div>



<br><br><br><br>
<button onclick="myFunction2()" class="dropbtn" style = "position:absolute; left:-1.5%; width:20%;">SUPPORT</button>
<div id="myDropdown2" class="dropdown-content2">
<ul>
<li><a>test.</a></li>
<li><a>test.</a></li>

</ul>
</div>
</div>


</div>
<script>

function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}

window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {

var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}

function myFunction2() {
document.getElementById("myDropdown2").classList.toggle("show");
}

window.onclick = function(event) {
if (!event.target.matches('.dropbtn'))
{

var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++)
{
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show'))
{
openDropdown.classList.remove('show');
}
}
}
}

</script>


</body>
</html>

最佳答案

假设您总是有一个按钮和一个按钮旁边带有不同 id 的下拉菜单。我认为这将是一个解决方案,而且它还可以避免 CSS 中的冗余。

function hideDropdown(exceptId){
var dropdowns = document.getElementsByClassName("dropdown-content");
for (var i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show') && openDropdown.id !== exceptId) {
openDropdown.classList.remove('show');
}
}
}

window.onclick = function(event) {
if (!event.target.classList.contains('dropbtn')) {
hideDropdown(null);
}else{
var dropdown = event.target.nextElementSibling;
dropdown.classList.toggle("show");
hideDropdown(dropdown.id);
}
}
.dropbtn {
background-color: #00BFFF;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
background-color: #1E90FF;
}

.dropdown2 {
position: relative;
display: block;
}

.dropdown {
position: relative;
display: block;
}


.dropdown-content {
display: none;
position: absolute;
background-color: #1E90FF;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;

}

.dropdown a:hover {
background-color: #00BFFF;

}

.show {
display:block;
position: relative;
left:18.6%;
top:-20%;
width: 80%;
}

.border2 {
float: left;
border: 2px #FA5858;
background-color: #FFFFFF;
margin: 0 auto;
padding-bottom:40px;
padding-top:5px;
width : 86%;
height : 44%;
top : -10%;
left : 7%;
position : relative;
padding: 1em;
}
<div class = "border2">
<div style = "font-size:25px;list-style-type: none;margin: 0;padding: 10px;overflow: hidden;background-color: #BDBDBD;
position : relative;top : -10%;width : 101.2%;left:-1.5%;"><strong>NEED HELP?</strong></div>

<div class="dropdown">

<button class="dropbtn" style = "position:absolute; left:-1.5%; width:20%;">GUIDES</button>
<div id="myDropdown" class="dropdown-content">
<ul>
<li><a>asasd.</a></li>
<li><a>Thasdat.</a></li>
<li><a>test2.</a></li>
</ul>
</div>



<br><br><br><br>
<button class="dropbtn" style = "position:absolute; left:-1.5%; width:20%;">SUPPORT</button>
<div id="myDropdown2" class="dropdown-content">
<ul>
<li><a>test.</a></li>
<li><a>test.</a></li>

</ul>
</div>
</div>


</div>

关于javascript - 如何正确处理多个下拉按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34976383/

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