admin管理员组文章数量:1433530
I'm trying to show/hide a drop down menu when hamburger icon is clicked by user.
Class .show is toggled on #dropdown-content when user clicks on #hamburger-icon. Toggling .show is done to show/hide #dropdown-content.
Please refer to jsfiddle: /
Relevant JS code is:
// Get the button, and when the user clicks on it, execute myFunction
document.getElementById("hamburger-icon").onclick = function() {myFunction()};
/* myFunction toggles between adding and removing the show class, which is used to hide and show the dropdown content */
function myFunction() {
document.getElementById("dropdown-content").classList.toggle("show");
}
I expect when hamburger icon is clicked by user, links inside #dropdown-content will be visible if it was hidden, and hidden if it was visible. But #dropdown-content is not made visible/invisible when user clicks on the hamburger icon.
I'm trying to show/hide a drop down menu when hamburger icon is clicked by user.
Class .show is toggled on #dropdown-content when user clicks on #hamburger-icon. Toggling .show is done to show/hide #dropdown-content.
Please refer to jsfiddle: https://jsfiddle/d8oubjmk/1/
Relevant JS code is:
// Get the button, and when the user clicks on it, execute myFunction
document.getElementById("hamburger-icon").onclick = function() {myFunction()};
/* myFunction toggles between adding and removing the show class, which is used to hide and show the dropdown content */
function myFunction() {
document.getElementById("dropdown-content").classList.toggle("show");
}
I expect when hamburger icon is clicked by user, links inside #dropdown-content will be visible if it was hidden, and hidden if it was visible. But #dropdown-content is not made visible/invisible when user clicks on the hamburger icon.
Share Improve this question edited Feb 4, 2019 at 7:49 Logan Lee asked Feb 4, 2019 at 7:43 Logan LeeLogan Lee 651 gold badge2 silver badges7 bronze badges 1- Possible duplicate of show/hide drop down – moinmaroofi Commented Feb 4, 2019 at 7:50
1 Answer
Reset to default 1You're missing just 2 things:
Wrap your JS code in window.onload
to ensure the DOM elements are actually loaded before you attempt to select them.
window.onload = () => {
// Get the button, and when the user clicks on it, execute myFunction
document.getElementById("hamburger-icon").onclick = function() {myFunction()};
/* myFunction toggles between adding and removing the show class, which is used to hide and show the dropdown content */
function myFunction() {
document.getElementById("dropdown-content").classList.toggle("show");
}
}
Your .show
class needs to be more specific otherwise it loses specificity to the #dropdown-content
and has no effect.
/* Applies to elements that have id="dropdown-content" and class="show" */
#dropdown-content.show
{
display: block;
}
Here's your code working:
window.onload = () => {
// Get the button, and when the user clicks on it, execute myFunction
document.getElementById("hamburger-icon").onclick = function() {myFunction()};
/* myFunction toggles between adding and removing the show class, which is used to hide and show the dropdown content */
function myFunction() {
document.getElementById("dropdown-content").classList.toggle("show");
}
}
#dropdown-content a
{
text-decoration:none;
display:block;
}
#dropdown-content {
display: none;
}
#dropdown-content.show
{
display: block;
}
<div id="top-menu">
<div id="hamburger-icon">☰</div>
<div id="dropdown-content" class="show-hide-menu">
<a href="#">Home</a>
<a href="#">Menu1</a>
<a href="#">Menu2</a>
<a href="#">Menu3</a>
<a href="#">About Us</a>
</div>
</div>
本文标签: javascriptHow to use classList toggle to showhide drop down list contentStack Overflow
版权声明:本文标题:javascript - How to use classList toggle to showhide drop down list content? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745612772a2666214.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论