单击菜单在菜单外关闭不正常的菜单 - buttOneVent.Matches如果不是功能
单击菜单按钮外时应该关闭的下拉菜单不起作用。我以前遇到了这个问题,但是问题重新解决了,无法找到解决方案。帮助您将不胜感激。我不确定该代码为什么不起作用,因此澄清会有所帮助。
let btnn = document.getElementById("btn");
btnn.addEventListener('click', function() {
let dropdown = document.getElementById('dropdown');
dropdown.classList.toggle("visible");
})
window.onclick = function(event){
if (!event.target.matches('#btn')) {
let dropd = document.getElementById('dropdown');
dropd.classList.remove("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;
margin-top: 0;
margin-bottom: 0;
border: none;
background-color: red;
padding: 0;
}
nav {
display: block;
background-color: black;
width: 100vw;
}
nav ul {
display: none;
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;
}
.visible {
display: block;
}
.fa-bars:before, .fa-navicon:before {
content: "\f0c9";
font-size: 50px;
line-height: 10vh;
}
@media only screen and (min-width: 480px) {
.fa-bars:before, .fa-navicon:before {
font-size: 35px;
}
/* #dropdown {
display: block;
} */
}
@media only screen and (min-width: 600px) {
.fa-bars:before, .fa-navicon:before {
font-size: 30px;
}
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<!-- <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"></i></button>
</div>
<nav>
<ul id="dropdown">
<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>
The dropdown menu which is supposed to close when clicking outside of the menu button is not working. I have had this problem before but the problem re-occurred and can't get to a solution. Help would be appreciated. I am not sure as to why the code doesn't work, so some clarification would be helpful.
let btnn = document.getElementById("btn");
btnn.addEventListener('click', function() {
let dropdown = document.getElementById('dropdown');
dropdown.classList.toggle("visible");
})
window.onclick = function(event){
if (!event.target.matches('#btn')) {
let dropd = document.getElementById('dropdown');
dropd.classList.remove("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;
margin-top: 0;
margin-bottom: 0;
border: none;
background-color: red;
padding: 0;
}
nav {
display: block;
background-color: black;
width: 100vw;
}
nav ul {
display: none;
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;
}
.visible {
display: block;
}
.fa-bars:before, .fa-navicon:before {
content: "\f0c9";
font-size: 50px;
line-height: 10vh;
}
@media only screen and (min-width: 480px) {
.fa-bars:before, .fa-navicon:before {
font-size: 35px;
}
/* #dropdown {
display: block;
} */
}
@media only screen and (min-width: 600px) {
.fa-bars:before, .fa-navicon:before {
font-size: 30px;
}
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<!-- <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"></i></button>
</div>
<nav>
<ul id="dropdown">
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我更好地重组您的JavaScript。我认为它将解决您的问题,代码也很精确。
在您以前的代码中,我相信您的按钮点击被窗口点击覆盖。在这里,我要单击身体,只需匹配按钮ID。如果是匹配的,那么我正在执行切换,否则,我正在删除课程。
***我使用了parenthnode。如果您想知道为什么我可以简单地使用log即可查看按钮点击的位置。您会看到点击将在按钮内获取图标。这就是为什么我带上图标父母来检查我想要的点击是否发生。
let me restructure your javascript better. It will solve your problem and the code is precise too I think.
In your previous code, I believe your button click was overridden by the window click. Here, I'm clicking on the body and simply matching the button id. If it matches, then I'm doing the toggle and otherwise, I'm removing the class.
*** I've used parentNode. If you're wondering why I'd used that you can just simply console log to see where the button click happens. You will see that the click is getting the icon inside the button. That is why I took the icon parent to check if the click is happening where I want it.
您可以使用
document.body.ddeventlistener('click',fn,true);
。您的JS文件应该像这样You can use
document.body.addEventListener('click', fn, true);
. Your js file should be like this maybe