[英]Remove active class / JS Navigation
I have been trying for hours now, but can not figure my way out of this one.我现在已经尝试了几个小时,但无法找到解决这个问题的方法。 The Menu contains a regular submenu which gets an "active" class. Great, done with foreach.
菜单包含一个常规子菜单,它获得“活动”class。太棒了,用 foreach 完成了。 But how can I remove the active class when the user clicks anywhere in the document?
但是,当用户在文档中的任意位置单击时,如何删除活动的 class 呢?
If I put an id on the body that removes the "active" class with an event listener, it will be removed without being present.如果我在主体上放置一个 id 以使用事件侦听器删除“活动的”class,它将在不存在的情况下被删除。 Should I use "contains()"?
我应该使用“contains()”吗? Tried but con not get it to work either.
尝试过但也无法使其正常工作。 Code is as plain as possible.
代码尽可能简单。 Thank you for any help!!!
感谢您的任何帮助!!!
Link to Codepen: https://codepen.io/jaeiko/pen/OJOZRgm Codepen链接: https://codepen.io/jaeiko/pen/OJOZRgm
<div id="nav__menu">
<ul class="navigation__desktop">
<li> <a href="#"> MenuItemOne</a></li>
<li> <a class="drop-down-items sub" href="#"> MenuItemTwo/Sub</a>
<ul class="navigation__desktop__dropdown">
<li> <a href="#"> SubMenuOne</a></li>
<li> <a href="#"> SubMenuTwo</a></li>
<li> <a href="#"> SubMenuThree</a></li>
<li> <a href="#"> SubMenuFour</a></li>
<li> <a href="#"> SubMenuFive</a></li>
<li> <a href="#"> SubMenuSix</a></li>
<li> <a href="#"> SubMenuSeven</a></li>
</ul>
</li>
<li> <a href="#"> MenuItemTwo</a></li>
<li> <a class="drop-down-items sub" href="#"> MenuItemThree/Sub</a>
<ul id="service-submenu" class="navigation__desktop__dropdown">
<li> <a href="#"> SubMenuEight</a></li>
<li> <a href="#"> SubMenuNine</a></li>
<li> <a href="#"> SubMenuTen</a></li>
</ul>
</li>
<li> <a href="#"> MenuItemFour</li></a>
</ul>
</div>
////// SCSS
.navigation__desktop {
display: flex;
justify-content: space-evenly;
position: relative;
a {
padding: 0.5rem;
display: block;
text-decoration: none;
font-weight: bold;
font-family: sans-serif;
}
}
.navigation__desktop__dropdown {
position: absolute;
display: flex;
flex-direction: column;
background-color: white;
border-radius: 0.1rem;
box-shadow: 0 0 10px #718096;
display: none;
text-transform: none;
}
.active {
display: block;
}
li {
list-style-type: none;
}
.sub {
color: red;
}
///////////// JS
/// Variables
let dropDownItems = document.querySelectorAll(".drop-down-items");
let dropDownUL = document.querySelectorAll(".navigation__desktop__dropdown");
/// Dropdown
dropDownItems.forEach(item => {
item.addEventListener("click", (e) => {
e.preventDefault();
let showMenuItem = item.nextElementSibling;
showMenuItem.classList.add("active");
})
})
[1]: https://codepen.io/jaeiko/pen/OJOZRgm
In your case i would create a global click event and check if the clicked element is a link, if not remove the active class for others.在您的情况下,我将创建一个全局点击事件并检查点击的元素是否是链接,如果不是,则为其他人删除活动的 class。
/// Variables let dropDownItems = document.querySelectorAll(".drop-down-items"); let dropDownUL = document.querySelectorAll(".navigation__desktop__dropdown"); /// Dropdown dropDownItems.forEach(item => { item.addEventListener("click", (e) => { e.preventDefault(); let showMenuItem = item.nextElementSibling; showMenuItem.classList.add("active"); }) }) // Add a global click handler window.addEventListener('click', (e) => { if (e.target.tagName.== 'A') { dropDownItems.forEach((item) => { let shownMenuItem = item;nextElementSibling. if (shownMenuItem.classList.contains('active')) { shownMenuItem.classList;remove('active'); } }); } });
.navigation__desktop { display: flex; justify-content: space-evenly; position: relative; } a { padding: 0.5rem; display: block; text-decoration: none; font-weight: bold; font-family: sans-serif; }.navigation__desktop__dropdown { position: absolute; display: flex; flex-direction: column; background-color: white; border-radius: 0.1rem; box-shadow: 0 0 10px #718096; display: none; text-transform: none; }.active { display: block; } li { list-style-type: none; }.sub { color: red; }
<div id="nav__menu"> <ul class="navigation__desktop"> <li> <a href="#"> MenuItemOne</a></li> <li> <a class="drop-down-items sub" href="#"> MenuItemTwo/Sub</a> <ul class="navigation__desktop__dropdown"> <li> <a href="#"> SubMenuOne</a></li> <li> <a href="#"> SubMenuTwo</a></li> <li> <a href="#"> SubMenuThree</a></li> <li> <a href="#"> SubMenuFour</a></li> <li> <a href="#"> SubMenuFive</a></li> <li> <a href="#"> SubMenuSix</a></li> <li> <a href="#"> SubMenuSeven</a></li> </ul> </li> <li> <a href="#"> MenuItemTwo</a></li> <li> <a class="drop-down-items sub" href="#"> MenuItemThree/Sub</a> <ul id="service-submenu" class="navigation__desktop__dropdown"> <li> <a href="#"> SubMenuEight</a></li> <li> <a href="#"> SubMenuNine</a></li> <li> <a href="#"> SubMenuTen</a></li> </ul> </li> <li> <a href="#"> MenuItemFour</li></a> </ul> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.