gpt4 book ai didi

javascript - 下拉 window.onclick javascript 仅适用于一个菜单

转载 作者:行者123 更新时间:2023-11-28 14:40:46 25 4
gpt4 key购买 nike

如果只有一个菜单,此菜单有效。但如果我想做其中的两个,window.onclick = function(event) 仅适用于一个菜单。为什么会这样并且很容易修复?我怎样才能使 2 个菜单工作相同?.. 请只使用 javascript.. 没有 jquery。

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

// Close the dropdown if the user clicks outside of it
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');
}
}
}
}
.dropbtn {
background-color: #6dc14b;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
width:100%;
}

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

.dropdown {
width:300px;
margin:0 auto;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #ddd;
min-width: 300px;
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: #b9d8eb;}

.show {display: block;}

}
<div class="dropdown">
<button onclick="openDrop()" class="dropbtn">Company A</button>
<div id="myDropdown" class="dropdown-content">
<a>Type 1</a>
<a href="#contact"> Type 2</a>
</div>
</div>

最佳答案

您需要将下拉列表的id 作为openDrop() 函数的参数 传递,这里有一个快速解决方法:

function openDrop(menu) {
document.getElementById(menu).classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
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');
}
}
}
}
.dropbtn {
background-color: #6dc14b;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
width:100%;
}

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

.dropdown {
width:300px;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #ddd;
min-width: 300px;
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: #b9d8eb;}

.show {display: block;}

}
<div class="dropdown">
<button onclick="openDrop('myDropdown1')" class="dropbtn">Company A</button>
<div id="myDropdown1" class="dropdown-content">
<a>Type 1</a>
<a href="#contact"> Type 2</a>
</div>
</div>

<div class="dropdown" style="float:right">
<button onclick="openDrop('myDropdown2')" class="dropbtn">Company B</button>
<div id="myDropdown2" class="dropdown-content">
<a>Type 3</a>
<a href="#contact"> Type 4</a>
</div>
</div>

关于javascript - 下拉 window.onclick javascript 仅适用于一个菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52958650/

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