gpt4 book ai didi

Javascript - 单击空格时左侧下拉菜单不会消失,但尽管代码相同,但右侧下拉菜单会消失。使困惑

转载 作者:行者123 更新时间:2023-11-30 14:17:10 24 4
gpt4 key购买 nike

当我点击选择条件框时,会出现一个下拉菜单。当我单击空白区域或数据表按钮时,下拉菜单消失 = 好。当我对“选择数据表”执行相同操作时,下拉菜单不会消失,除非我再次单击该按钮,我做错了什么?我想知道是不是因为我有相同的代码...如果我删除右侧窗口的 JS,该 JS 允许窗口在单击空白处消失,则相反的情况发生(左侧框的菜单消失但不是右侧)。完整代码如下:

/* When the user clicks on the button, toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDatalist").classList.toggle("show")
}

window.onclick = function(event) {
if (!event.target.matches('.dropdata')) {
var dropdowns = document.getElementsByClassName("datalist-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}

function criFunction() {
document.getElementById("myCriteria").classList.toggle("show")
}

window.onclick = function(event) {
if (!event.target.matches('.dropcriteria')) {
var dropdowns = document.getElementsByClassName("criterialist-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
/*START Title*/

h1 {
color: #46b3d1;
font-family: Gotham;
font-weight: 80;
}

/*END title*/

/*START Database drop down*/
.dropdata {
font-family: Gotham;
color: white;
padding: 16px;
font-size: 16px;
width:200px;
border: none;
cursor: pointer;
background-color: #46b3d1;
}


.dropdata:hover, .dropdata:focus {
background-color: #22819b
}



.datalist {
position: relative;
display: inline-block;
font-family: gotham;
}


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

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

.datalist a:hover {background-color: #ddd;}
/*END Database drop down*/


/*START Criteria box */

.dropcriteria {
font-family: Gotham;
color: white;
padding: 16px;
font-size: 16px;
width:200px;
border: none;
cursor: pointer;
background-color: #46b3d1;
}


.dropcriteria:hover, .dropcriteria:focus {
background-color: #22819b
}


.criterialist {
position: relative;
display: inline-block;
font-family: gotham;
}


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

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

.criterialist a:hover {background-color: #ddd;}

.show {display: block;}


/*END Criteria box */
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Project Eric</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<h1>Project Eric</h1>

<div class="datalist">
<button onclick="myFunction()" class="dropdata">Choose data table</button>
<div id="myDatalist" class="datalist-content">
<a href="oxford_ann">Oxford Annual</a>
<a href="eng_counties">English Counties</a>
<a href="oxford_qu">Oxford Quarterly</a>
</div>
</div>

<div class="criterialist">
<button onclick="criFunction()" class="dropcriteria">Choose criteria</button>
<div id="myCriteria" class="criterialist-content">
<a href="index">Index</a>
<a href="database">Database</a>
<a href="filter">Filter</a>
</div>
</div>

最佳答案

问题是点击发生时只有第一个window.onclick函数被触发。

将逻辑组合到一个方法中,它将按预期工作。

/* When the user clicks on the button, toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDatalist").classList.toggle("show")
}

window.onclick = function(event) {
if (!event.target.matches('.dropcriteria')) {
var dropdowns = document.getElementsByClassName("criterialist-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}

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

function criFunction() {
document.getElementById("myCriteria").classList.toggle("show")
}
/*START Title*/

h1 {
color: #46b3d1;
font-family: Gotham;
font-weight: 80;
}

/*END title*/

/*START Database drop down*/
.dropdata {
font-family: Gotham;
color: white;
padding: 16px;
font-size: 16px;
width:200px;
border: none;
cursor: pointer;
background-color: #46b3d1;
}


.dropdata:hover, .dropdata:focus {
background-color: #22819b
}



.datalist {
position: relative;
display: inline-block;
font-family: gotham;
}


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

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

.datalist a:hover {background-color: #ddd;}
/*END Database drop down*/


/*START Criteria box */

.dropcriteria {
font-family: Gotham;
color: white;
padding: 16px;
font-size: 16px;
width:200px;
border: none;
cursor: pointer;
background-color: #46b3d1;
}


.dropcriteria:hover, .dropcriteria:focus {
background-color: #22819b
}


.criterialist {
position: relative;
display: inline-block;
font-family: gotham;
}


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

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

.criterialist a:hover {background-color: #ddd;}

.show {display: block;}


/*END Criteria box */
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Project Eric</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<h1>Project Eric</h1>

<div class="datalist">
<button onclick="myFunction()" class="dropdata">Choose data table</button>
<div id="myDatalist" class="datalist-content">
<a href="oxford_ann">Oxford Annual</a>
<a href="eng_counties">English Counties</a>
<a href="oxford_qu">Oxford Quarterly</a>
</div>
</div>

<div class="criterialist">
<button onclick="criFunction()" class="dropcriteria">Choose criteria</button>
<div id="myCriteria" class="criterialist-content">
<a href="index">Index</a>
<a href="database">Database</a>
<a href="filter">Filter</a>
</div>
</div>

关于Javascript - 单击空格时左侧下拉菜单不会消失,但尽管代码相同,但右侧下拉菜单会消失。使困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53397424/

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