除了@Elezar解决了这里提出的问题:
CSS focus does not change color after second clicking on dropdown menu
以下示例:
//dropdown_1
function dropdown_one(btn) {
document.getElementById("menu_list").classList.toggle("dcontent");
btn.classList.toggle("button-selected");
}
window.onclick = function(event) {
if (!event.target.matches('.button')) {
var menu = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < menu.length; i++) {
var open_list = menu[i];
if (open_list.classList.contains('dcontent')) {
open_list.classList.remove('dcontent');
}
}
let selected = document.getElementsByClassName("button-selected");
for (let i = 0, len = selected.length; i < len; i++) {
selected[i].classList.remove("button-selected");
}
}
}
//dropdown_2
function dropdown_two(btn) {
document.getElementById("menu_list2").classList.toggle("dcontent");
btn.classList.toggle("button-selected2");
}
window.onclick = function(event) {
if (!event.target.matches('.button2')) {
var menu = document.getElementsByClassName("dropdown-content2");
var i;
for (i = 0; i < menu.length; i++) {
var open_list = menu[i];
if (open_list.classList.contains('dcontent')) {
open_list.classList.remove('dcontent');
}
}
let selected2 = document.getElementsByClassName("button-selected2");
for (let i = 0, len = selected2.length; i < len; i++) {
selected2[i].classList.remove("button-selected2");
}
}
}
&#13;
.button, .button2 {
background-color: blue;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.button:hover, .button2:hover {
background-color: green;
}
.button-selected,
.button-selected:hover {
background-color: red;
}
.button-selected2,
.button-selected2:hover {
background-color: orange;
}
.dropdown, .dropdown2 {
position: relative;
display: inline;
}
.dropdown-content, .dropdown-content2 {
display: none;
position: absolute;
background-color: #efefef;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
left: 30px;
}
.dropdown-content a, .dropdown-content2 a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover, .dropdown2 a:hover {
background-color: grey
}
.dcontent {
display: block;
}
&#13;
<div class="dropdown">
<button onclick="dropdown_one(this)" class="button">Dropdown1</button>
<div id="menu_list" class="dropdown-content">
<a href="#">one</a>
<a href="#">two</a>
<a href="#">three</a>
</div>
</div>
<div class="dropdown2">
<button onclick="dropdown_two(this)" class="button2">Dropdown2</button>
<div id="menu_list2" class="dropdown-content2">
<a href="#">four</a>
<a href="#">five</a>
<a href="#">six</a>
</div>
</div>
&#13;
结果很奇怪。
在Dropdown_1下拉列表打开时,列表在元素外部或Dropdown_2上单击时,列表不会消失。
我知道函数组合有一些东西,但我需要帮助。如果需要,我也想知道如何添加更多下拉列表。
谢谢你,
CP
答案 0 :(得分:0)
我实际上找到了一个解决方案,问题是第二个下拉按钮条件必须放在window.onclick = function(event)中,而不是单独的。
//dropdowns
function dropdown_one(btn) {
document.getElementById("menu_list").classList.toggle("dcontent");
btn.classList.toggle("button-selected");
}
function dropdown_two(btn) {
document.getElementById("menu_list2").classList.toggle("dcontent");
btn.classList.toggle("button-selected2");
}
//condition
window.onclick = function(event) {
if (!event.target.matches('.button')) {
var menu = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < menu.length; i++) {
var open_list = menu[i];
if (open_list.classList.contains('dcontent')) {
open_list.classList.remove('dcontent');
}
}
let selected = document.getElementsByClassName("button-selected");
for (let i = 0, len = selected.length; i < len; i++) {
selected[i].classList.remove("button-selected");
}
}
if (!event.target.matches('.button2')) {
var menu = document.getElementsByClassName("dropdown-content2");
var i;
for (i = 0; i < menu.length; i++) {
var open_list = menu[i];
if (open_list.classList.contains('dcontent')) {
open_list.classList.remove('dcontent');
}
}
let selected2 = document.getElementsByClassName("button-selected2");
for (let i = 0, len = selected2.length; i < len; i++) {
selected2[i].classList.remove("button-selected2");
}
}
}
.button, .button2 {
background-color: blue;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.button:hover, .button2:hover {
background-color: green;
}
.button-selected,
.button-selected:hover {
background-color: red;
}
.button-selected2,
.button-selected2:hover {
background-color: orange;
}
.dropdown, .dropdown2 {
position: relative;
display: inline;
}
.dropdown-content, .dropdown-content2 {
display: none;
position: absolute;
background-color: #efefef;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
left: 30px;
}
.dropdown-content a, .dropdown-content2 a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover, .dropdown2 a:hover {
background-color: grey
}
.dcontent {
display: block;
}
<div class="dropdown">
<button onclick="dropdown_one(this)" class="button">Dropdown1</button>
<div id="menu_list" class="dropdown-content">
<a href="#">one</a>
<a href="#">two</a>
<a href="#">three</a>
</div>
</div>
<div class="dropdown2">
<button onclick="dropdown_two(this)" class="button2">Dropdown2</button>
<div id="menu_list2" class="dropdown-content2">
<a href="#">four</a>
<a href="#">five</a>
<a href="#">six</a>
</div>
</div>