Закрытие нескольких div при нажатии вне блоков
Имеется динамическое меню с несколькими выпадающими подменю. Как сделать чтобы при нажатии на пустое место открытое меню закрывалось у всех элементов? (Foreach)
const btnMenu = document.querySelectorAll(".header_nav__item--has-sub-menu");
const menu = document.querySelectorAll(".header_content");
const isMenuShow = false;
btnMenu.forEach((button) => {
button.addEventListener("click", function (e) {
e.stopPropagation();
menu.forEach((item) => {
item.classList.remove("active_content");
});
button.addEventListener("click", function (e) {
e.stopPropagation();
button.children[2].classList.toggle("active_content");
});
// button.children[2].classList.remove("active_content");
button.children[2].classList.toggle("active_content");
});
});
document.addEventListener("click", function (e) {
const target = e.target;
const its_menu = target == menu || menu.forEach((item) => {
item.contains(target);
});
const its_btnMenu = target == btnMenu.target;
const menu_is_active = menu.forEach((item) => {
item.classList.contains("active_content");
console.log(item.classList.contains("active_content"))
});
if (!its_menu && !its_btnMenu && menu_is_active) {
menu.forEach((item) => {
item.classList.remove("active_content");
});
}
});
.header {
width: 100%;
background: #000000;
z-index: 9999;
padding: 30px 0;
}
.header.hid{
display: none;
}
.header-btn{
background-color: #3E30AE;
color: #fff !important;
border-color:#3E30AE;
}
.header_wrapper {
display: flex;
justify-content: space-between;
align-items: center;
max-width: 1440px;
width: 100%;
height: auto;
margin: 0 auto;
}
.header_nav {
display: flex;
align-items: center;
max-width: 715px;
justify-content: center;
width: 100%;
}
.header_nav__item, .header_nav__item a {
/*position: relative;*/
font-style: normal;
font-weight: 500;
font-size: 18px;
line-height: 24px;
text-align: center;
color: red;
margin-right: 30px;
display: flex;
align-items: center;
}
.header_nav__item a {
display: inline-block;
margin-right: 7px;
}
.header_nav__item:last-child {
margin-right: 0;
}
.header_inner {
display: flex;
align-items: center;
}
.header_lang {
font-style: normal;
font-weight: 500;
font-size: 18px;
line-height: 24px;
text-align: center;
color: #FFFFFF;
margin-right: 33px;
cursor: pointer;
}
.header_schedule {
font-style: normal;
font-weight: 500;
font-size: 18px;
line-height: 24px;
text-align: center;
color: #FFFFFF;
background: #3E30AE;
border-radius: 90px;
width: 209px;
height: 50px;
display: flex;
justify-content: center;
cursor: pointer;
align-items: center;
}
.header_schedule span {
display: inline-block;
margin-right: 20px;
}
.header_content {
background: #000000;
width: 100%;
margin-top: -1px;
height: 0;
display: none;
transition: height 0.3s ease;
}
.header_content.active_content {
height: auto;
display: block;
}
.header_content--item{
position: absolute;
top: 100%;
left: 0;
right: 0;
text-align: left;
}
.header_content_items {
padding: 20px 0 0;
margin: 0 auto;
width: 100%;
display: flex;
flex-wrap: wrap;
max-width: 1100px;
}
.header_content-mob {
display: none !important;
}
.header_content_item {
padding: 20px;
transition: background-color 0.3s ease;
cursor: pointer;
width: 100%;
display: block;
max-width: 220px;
}
.header_content_item:hover {
background: #0F0E20;
}
.header_content_item:hover > .header_content-title img {
opacity: 1;
}
.header_content-title {
font-style: normal;
font-weight: 500;
font-size: 24px;
line-height: 30px;
color: #FFFFFF;
margin-bottom: 13px;
display: flex;
align-items: center;
justify-content: flex-end;
flex-direction: row-reverse;
}
.header_content-title img {
margin-left: 10px;
opacity: 0;
transition: opacity 0.3s ease;
}
.header-menu-button {
display: none;
}
.header_content-text {
font-style: normal;
font-weight: 400;
font-size: 14px;
line-height: 18px;
color: #ABAAC0;
}
.header_right {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
margin-left: 123px;
}
.header_nav__item--mob {
display: none;
}
<header class="header"><nav class="header_nav">
<div class="header_nav__item header_nav__item--has-sub-menu" onclick="return false">
<a href="">All Solutions</a>
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="8" viewBox="0 0 14 8" fill="none">
<path d="M7.69338 7.3328C7.3062 7.70536 6.6938 7.70536 6.30662 7.3328L0.993849 2.22058C0.345179 1.59639 0.787015 0.5 1.68722 0.5L12.3128 0.500001C13.213 0.500001 13.6548 1.5964 13.0062 2.22058L7.69338 7.3328Z" fill="white"></path>
</svg>
<div class="header_content header_content--item content--submenu ">
<div class="header_content_items">
<div class="header_content_item">
<a href="" target="_blank" class="header_content-title">title</a>
</div>
<div class="header_content_item">
<a href="" target="_blank" class="header_content-title">title</a>
</div>
<div class="header_content_item">
<a href="" target="_blank" class="header_content-title">title</a>
</div>
<div class="header_content_item">
<a href="" target="_blank" class="header_content-title">title</a>
</div>
</div>
</div>
</div>
<div class="header_nav__item header_nav__item--has-sub-menu" onclick="return false">
<a href="">About us</a>
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="8" viewBox="0 0 14 8" fill="none">
<path d="M7.69338 7.3328C7.3062 7.70536 6.6938 7.70536 6.30662 7.3328L0.993849 2.22058C0.345179 1.59639 0.787015 0.5 1.68722 0.5L12.3128 0.500001C13.213 0.500001 13.6548 1.5964 13.0062 2.22058L7.69338 7.3328Z" fill="white"></path>
</svg>
<div class="header_content header_content--item content--submenu">
<div class="header_content_items">
<div class="header_content_item">
<a href="ttttt" target="_blank" class="header_content-title">title</a>
</div>
</div>
</div>
</div>
<div class="header_nav__item ">
<a href="#contacts">Contact Us</a>
<div class="header_content header_content--item content--submenu">
<div class="header_content_items">
</div>
</div>
</div>
<div class="header_content header_content-mob">
<div class="header_content_items">
<a href="" target="_blank" class="header_content_item">
<div class="header_content-title">title</div>
</a>
<a href="" target="_blank" class="header_content_item">
<div class="header_content-title">title</div>
</a>
</div>
</div>
</nav>
</header>