gpt4 book ai didi

javascript - 当用户单击其他地方时,如何隐藏导航栏的下拉菜单?

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

我正在构建一个带有导航栏和下拉菜单的新网站,并希望在我单击另一个按钮或网站的其他部分时让下拉菜单消失。我该如何编码?

下面的代码是我试图让它工作的最新实验。当它是一个按钮时它工作得很好,但我一直无法将它扩展到多个按钮。我对此很陌生,正在努力学习。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.navbar {
overflow: hidden;
background-color: #333;
font-family: Arial, Helvetica, sans-serif;
}

.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

.dropdown {
float: left;
overflow: hidden;
}

.dropdown .dropbtn {
cursor: pointer;
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}

.navbar a:hover, .dropdown:hover .dropbtn, .dropbtn:focus {
background-color: red;
}

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

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

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

.show {
display: block;
}

.wrapper {
min-width: 800px;
margin: auto;
}
</style>
</head>
<body>

<div class="wrapper">
<div class="navbar">

<a href="#home">Company name</a>

<div class="dropdown">
<button class="dropbtn" onclick="myFunction(1)">Our Methods
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content" id="myDropdown1">
<a href="#link">Insurance</a>
<a href="#link">Divorce</a>
<a href="#link">Financial Planning</a>
<a href="#link">Mortgage</a>
<a href="#link">Stocks</a>
<a href="#link">Pension</a>
</div>
</div>

<div class="dropdown">
<button class="dropbtn" onclick="myFunction(2)">Who we are
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content" id="myDropdown2">
<a href="#link">About the Team</a>
<a href="#link">Awards</a>
<a href="#link">Interesting articles</a>
</div>
</div>

<div class="dropdown">
<button class="dropbtn" onclick="myFunction(3)">Business info
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content" id="myDropdown3">
<a href="#link">Prices</a>
<a href="#link">Privacy</a>
<a href="#link">Methods</a>
</div>
</div>

<a href="#home">Recommendations</a>

<div class="dropdown">
<button class="dropbtn" onclick="myFunction(4)">Advice Evenings
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content" id="myDropdown4">
<a href="#link">Succesful Pensions</a>
<a href="#link">Casestudie Pension</a>
<a href="#link">Business Contacts</a>
</div>
</div>
</div>
</div>

<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction(num) {

document.getElementById("myDropdown" + num).classList.toggle("show");
}



// Close the dropdown if the user clicks outside of it
window.onclick = function(e) {
if (!e.target.matches('.dropbtn')) {
var myDropdown1 = document.getElementById("myDropdown1");
if (myDropdown1.classList.contains('show')) {
myDropdown1.classList.remove('show');
}
var myDropdown2 = document.getElementById("myDropdown2");
if (myDropdown2.classList.contains('show')) {
myDropdown2.classList.remove('show');
}
var myDropdown3 = document.getElementById("myDropdown3");
if (myDropdown3.classList.contains('show')) {
myDropdown3.classList.remove('show');
}
var myDropdown4 = document.getElementById("myDropdown4");
if (myDropdown4.classList.contains('show')) {
myDropdown4.classList.remove('show');
}
}
}

</script>
</body>
</html>

我希望当我点击别处时所有按钮都被隐藏,但不知何故这并没有发生。 (您可以在此处查看实际代码:http://growtricks.com/dropdown/dropdown4.html)。

我试过在 MyFunction 的开头包含这一行:

document.getElementsByClassName('dropdown-content').forEach(function(dropdown) { dropdown.classList.remove('show'); });

但这只是完全停止显示下拉内容。

  1. 当我点击别处时,如何使下拉内容消失?
  2. 如何让下拉内容在我点击另一个内容时消失?
  3. 这是一个额外的问题,但不是必需的:我如何清理最后的 window.onclick,使我不会重复相同的代码 n 次?

最佳答案

有多种方法。我有点喜欢有单独的函数变量来清除以前的操作:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.navbar {
overflow: hidden;
background-color: #333;
font-family: Arial, Helvetica, sans-serif;
}

.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

.dropdown {
float: left;
overflow: hidden;
}

.dropdown .dropbtn {
cursor: pointer;
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}

.navbar a:hover, .dropdown:hover .dropbtn, .dropbtn:focus {
background-color: red;
}

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

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

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

.show {
display: block;
}

.wrapper {
min-width: 800px;
margin: auto;
}
</style>
</head>
<body>

<div class="wrapper">
<div class="navbar">

<a href="#home" onclick="myFunction('home')">Company name</a>

<div class="dropdown">
<button class="dropbtn" onclick="myFunction(1)">Our Methods
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content" id="myDropdown1">
<a href="#link">Insurance</a>
<a href="#link">Divorce</a>
<a href="#link">Financial Planning</a>
<a href="#link">Mortgage</a>
<a href="#link">Stocks</a>
<a href="#link">Pension</a>
</div>
</div>

<div class="dropdown">
<button class="dropbtn" onclick="myFunction(2)">Who we are
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content" id="myDropdown2">
<a href="#link">About the Team</a>
<a href="#link">Awards</a>
<a href="#link">Interesting articles</a>
</div>
</div>

<div class="dropdown">
<button class="dropbtn" onclick="myFunction(3)">Business info
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content" id="myDropdown3">
<a href="#link">Prices</a>
<a href="#link">Privacy</a>
<a href="#link">Methods</a>
</div>
</div>

<a href="#home" onclick="myFunction('home')">Recommendations</a>

<div class="dropdown">
<button class="dropbtn" onclick="myFunction(4)">Advice Evenings
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content" id="myDropdown4">
<a href="#link">Succesful Pensions</a>
<a href="#link">Casestudie Pension</a>
<a href="#link">Business Contacts</a>
</div>
</div>
</div>
</div>

<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
let closeDropdown = function() {

}
function myFunction(num) {
closeDropdown();
let el = document.getElementById("myDropdown" + num);
if (el) {
el.classList.toggle("show");
closeDropdown = function() {
el.classList.remove("show");
}
} else {
closeDropdown = function() {
}
}
}



</script>
</body>
</html>

关于javascript - 当用户单击其他地方时,如何隐藏导航栏的下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58582128/

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