[英]How can I hide a HTML element when a link is selected with Javascript?
I wanted to create a responsive menu using purely HTML and CSS but an issue I've stumbled upon is that my website uses anchor links for the navigation, not external page links. 我想使用纯HTML和CSS创建一个响应式菜单,但是我偶然发现的一个问题是我的网站使用锚链接进行导航,而不是外部页面链接。 Because they are anchor links, when one is selected the menu overlay is still there.
因为它们是锚链接,所以当选择一个时,菜单覆盖仍然存在。
How can I use Javascript to add the display: none
style to the menu when a navigation link is selected? 选择导航链接后,如何使用Javascript添加
display: none
菜单display: none
样式? This will need to be able to be overridden again if the user reopens the navigation. 如果用户重新打开导航,则必须能够再次覆盖它。
Here's the basic HTML for my navigation: 这是我的导航基本HTML:
<label for="show-menu" class="show-menu">Menu</label>
<input type="checkbox" id="show-menu" role="button">
<nav>
<ul>
<li><a href="#projects">Projects</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
CSS: CSS:
/*Hide checkbox*/
input[type=checkbox]{
display: none;
}
/*Show menu when invisible checkbox is checked*/
input[type=checkbox]:checked ~ nav {
display: block;
}
一种轻松做到这一点的方法是使用jquery(如果您不使用jquery,我强烈建议您开始使用),只需将id =“ menuID”添加到菜单中:
<a href="url" onclick="$('#menuID').css('display', 'none');">Click me!</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.