I want to make a button that can pop up a window when the button is clicked~
But when the window pops up, click the green menu, the menu will not disappear, only click again or any blank space around the menu! The
menu will be closed again, but I I wrote a judgment why this function cannot be realized? I hope to get your help, thank you.
const el = document.querySelector('.click')
const menu = document.querySelector('.menu');
el.onclick = function() {
menu.classList.toggle("showmenu");
}
window.onclick = function(e) {
if (!e.classList.contains('menu') || !e.classList.contains('menu_item')) {
menu.removeClass('showmenu');
}
}
.click {
background-color: yellow;
padding: 20px;
}
.menu {
display: inline-block;
background-color: green;
list-style-type: none;
display: none;
}
.menu a {
text-decoration: none;
color: #fff;
padding: 30px;
font-size: 20px;
}
.showmenu {
display: inline-block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" class="click">click</button>
<ul class="menu">
<li class="menu_item"><a href="#">item1</a></li>
<li class="menu_item"><a href="#">item2</a></li>
<li class="menu_item"><a href="#">item3</a></li>
</ul>