如何在香草JS中关闭下拉按钮?我目前有一个方法来打开下拉按钮,但没有办法关闭按钮。我想保持循环到位,但不确定如何实现关闭。处理这个问题的正确方法是什么?我很感激你的帮助。HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script
src="https://code.jquery.com/jquery-3.6.0.js"
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
crossorigin="anonymous"></script>
<script src="https://kit.fontawesome.com/36947df53d.js" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div class="logo">
<p id="logo">Logo</p>
<button id="btn"><i class="fa-solid fa-bars fa-2xl"></i></button>
</div>
<nav class ="">
<ul class ="">
<li><a href="#" class="link">Link 1</a></li>
<li><a href="#" class="link">Link 2</a></li>
<li><a href="#" class="link">Link 3</a></li>
</ul>
</nav>
<script src="script.js"></script>
</body>
</html>
JS
let btnn = document.getElementById("btn");
btnn.addEventListener("click", changeBtn);
function changeBtn() {
let links = document.getElementsByClassName('link');
for (let i = 0; i < links.length; i++) {
document.getElementsByClassName('link')[i].style.display = "block";
}
}
CSS
body {
height: 100vh;
box-sizing: border-box;
margin: 0;
padding: 0;
}
.logo {
display: block;
text-align: left;
background: red;
height: 10vh;
}
.logo #logo {
display: inline;
line-height: 10vh;
font-size: 3em;
margin-left: 0.8em;
}
button#btn {
display: inline;
float: right;
margin-right: 2em;
line-height: 10vh;
margin-top: 0;
margin-bottom: 0;
border: none;
background-color: red;
padding: 0;
}
nav {
display: block;
background-color: black;
width: 100vw;
}
nav ul {
display: block;
list-style-type: none;
margin: 0;
padding-left: 0;
}
nav ul li {
text-align: center;
}
.link {
display: none;
color: white;
font-size: 2.4em;
background-color: blue;
text-decoration: none;
width: 100vw;
height: 7vh;
line-height: 7vh;
border-bottom: 2px solid black;
text-align: center;
}
您只需要在JavaScript中为关闭按钮添加一个函数,其中在再次单击按钮时将显示设置为none。您可以通过单击窗口对象的任何位置来完成相同的操作。
下面是JavaScript部分修改后的代码,用于再次点击按钮后关闭菜单。
let btnn = document.getElementById("btn");
btnn.addEventListener("click", changeBtn);
function changeBtn() {
let links = document.getElementsByClassName('link');
for (let i = 0; i < links.length; i++) {
document.getElementsByClassName('link')[i].style.display = "block";
}
// close the menu after clicking again
btnn.removeEventListener("click", changeBtn);
btnn.addEventListener("click", closeBtn);
}
function closeBtn() {
let links = document.getElementsByClassName('link');
for (let i = 0; i < links.length; i++) {
document.getElementsByClassName('link')[i].style.display = "none";
}
// open the menu after clicking again
btnn.removeEventListener("click", closeBtn);
btnn.addEventListener("click", changeBtn);
}
body {
height: 100vh;
box-sizing: border-box;
margin: 0;
padding: 0;
}
.logo {
display: block;
text-align: left;
background: red;
height: 10vh;
}
.logo #logo {
display: inline;
line-height: 10vh;
font-size: 3em;
margin-left: 0.8em;
}
button#btn {
display: inline;
float: right;
margin-right: 2em;
line-height: 10vh;
margin-top: 0;
margin-bottom: 0;
border: none;
background-color: red;
padding: 0;
}
nav {
display: block;
background-color: black;
width: 100vw;
}
nav ul {
display: block;
list-style-type: none;
margin: 0;
padding-left: 0;
}
nav ul li {
text-align: center;
}
.link {
display: none;
color: white;
font-size: 2.4em;
background-color: blue;
text-decoration: none;
width: 100vw;
height: 7vh;
line-height: 7vh;
border-bottom: 2px solid black;
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script
src="https://code.jquery.com/jquery-3.6.0.js"
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
crossorigin="anonymous"
></script>
<script
src="https://kit.fontawesome.com/36947df53d.js"
crossorigin="anonymous"
></script>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div class="logo">
<p id="logo">Logo</p>
<button id="btn"><i class="fa-solid fa-bars fa-2xl"></i></button>
</div>
<nav class="">
<ul class="">
<li><a href="#" class="link">Link 1</a></li>
<li><a href="#" class="link">Link 2</a></li>
<li><a href="#" class="link">Link 3</a></li>
</ul>
</nav>
<script src="script.js"></script>
</body>
</html>
你可以在JS中使用一个简单的classList.toggle
函数,通过分配一个基类,然后一个"visible"类,只需在两者之间切换单击。参见:https://www.w3schools.com/howto/howto_js_toggle_class.asp
编辑:在forEach
中添加,以防您想要多个按钮
let btnn = document.querySelectorAll(".btn");
btnn.forEach(btnn => btnn.addEventListener('click', function() {
let links = document.getElementById('links');
links.classList.toggle("visible");
}))
body {
height: 100vh;
box-sizing: border-box;
margin: 0;
padding: 0;
}
.logo {
display: block;
text-align: left;
background: red;
height: 10vh;
}
.logo #logo {
display: inline;
line-height: 10vh;
font-size: 3em;
margin-left: 0.8em;
}
button#btn {
display: inline;
float: right;
margin-right: 2em;
line-height: 10vh;
margin-top: 0;
margin-bottom: 0;
border: none;
background-color: red;
padding: 0;
}
nav {
display: block;
background-color: black;
width: 100vw;
}
nav ul {
display: block;
list-style-type: none;
margin: 0;
padding-left: 0;
}
nav ul li {
text-align: center;
}
.link {
display: block;
color: white;
font-size: 2.4em;
background-color: blue;
text-decoration: none;
width: 100vw;
height: 7vh;
line-height: 7vh;
border-bottom: 2px solid black;
text-align: center;
}
.links {
display: none;
}
.links.visible {
display: block;
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<script src="https://kit.fontawesome.com/36947df53d.js" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div class="logo">
<p id="logo">Logo</p>
<button class="btn" id="btn"><i class="fa-solid fa-bars fa-2xl"></i></button>
</div>
<nav class="">
<ul class="links" id="links">
<li><a href="#" class="link">Link 1</a></li>
<li><a href="#" class="link">Link 2</a></li>
<li><a href="#" class="link">Link 3</a></li>
</ul>
</nav>
<script src="script.js"></script>
</body>
</html>
更简单的方法是为nav元素添加一个类,然后使用changeBtn
函数切换nav元素的可见性
我使用注释
突出显示了对代码的更改
let btnn = document.getElementById("btn");
btnn.addEventListener("click", changeBtn);
function changeBtn() { //changes to function
let main_nav = document.getElementById("main_nav");
if (main_nav.classList.contains("nav_active")) {
//when element has nav_active class, hide the nav element and remove
//the "nav-active" class
main_nav.style.display = "none";
main_nav.classList.remove("nav_active");
} else {
//when element doesn't have nav_active class, show the nav element
//and add the "nav-active" class
main_nav.style.display = "block";
main_nav.classList.add("nav_active");
}
}
body {
height: 100vh;
box-sizing: border-box;
margin: 0;
padding: 0;
}
.logo {
display: block;
text-align: left;
background: red;
height: 10vh;
}
.logo #logo {
display: inline;
line-height: 10vh;
font-size: 3em;
margin-left: 0.8em;
}
button#btn {
display: inline;
float: right;
margin-right: 2em;
line-height: 10vh;
margin-top: 0;
margin-bottom: 0;
border: none;
background-color: red;
padding: 0;
}
nav {
display: none;
background-color: black;
width: 100vw;
}
nav ul {
display: block;
list-style-type: none;
margin: 0;
padding-left: 0;
}
nav ul li {
text-align: center;
}
.link {
display: block;
/* Changed display: none to display: block */
color: white;
font-size: 2.4em;
background-color: blue;
text-decoration: none;
width: 100vw;
height: 7vh;
line-height: 7vh;
border-bottom: 2px solid black;
text-align: center;
}
<script src="https://kit.fontawesome.com/36947df53d.js" crossorigin="anonymous"></script>
<div class="logo">
<p id="logo">Logo</p>
<button id="btn"><i class="fa-solid fa-bars fa-2xl"></i></button>
</div>
<nav id="main_nav" class="">
<!-- added id to nav element -->
<ul class="">
<li><a href="#" class="link">Link 1</a></li>
<li><a href="#" class="link">Link 2</a></li>
<li><a href="#" class="link">Link 3</a></li>
</ul>
</nav>