本文介绍了单击后如何关闭此菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试自定义此菜单.
I am trying to customise This menu.
这是 html 标记:
<ul class="topnav" id="myTopnav">
<li id="links"><a href="#home">Home</a></li>
<li id="links"><a href="#news">News</a></li>
<li id="links"><a href="#contact">Contact</a></li>
<li id="links"><a href="#about">About</a></li>
<li class="icon">
<a href="javascript:void(0);" onclick="myFunction()">☰</a>
</li>
</ul>
CSS:
ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
ul.topnav li {float: left;}
ul.topnav li a {
display: inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
}
ul.topnav li a:hover {background-color: #555;}
ul.topnav li.icon {display: none;}
@media screen and (max-width:1180px) {
ul.topnav li:not(:first-child) {display: none;}
ul.topnav li.icon {
float: right;
display: inline-block;
}
}
@media screen and (max-width:680px) {
ul.topnav.responsive {position: relative;}
ul.topnav.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
ul.topnav.responsive li {
float: none;
display: inline;
}
ul.topnav.responsive li a {
display: block;
text-align: left;
}
}
还有 JS :
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
我正在尝试添加它以使菜单在用户单击任何列表项后关闭-
I am trying to add this to make the menu close after user clicks on any of the list items-
$(document).ready(function($) {
$('#links').click(function() {
$("#myTopnav").removeClass("topnav");
});
});
但这似乎不起作用.
知道如何解决这个问题吗?
Any idea how to fix this?
推荐答案
id
属性在同一个文档中应该是唯一的,用类替换重复的:
The id
attribute should be unique in same document replace duplicate ones by class :
<ul class="topnav" id="myTopnav">
<li class="links"><a href="#home">Home</a></li>
<li class="links"><a href="#news">News</a></li>
<li class="links"><a href="#contact">Contact</a></li>
<li class="links"><a href="#about">About</a></li>
<li class="icon">
<a href="javascript:void(0);" onclick="myFunction()">☰</a>
</li>
</ul>
在您的 JS 代码中使用类选择器 .
:
Use class selector .
in your JS code :
$(document).ready(function($) {
$('.links').click(function() {
$("#myTopnav").removeClass("responsive");
});
});
注意:您必须删除 responsive
类而不是 topnav
.
NOTE : You have to remove responsive
class instead of topnav
.
希望这会有所帮助.
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
$(document).ready(function($) {
$('.links').click(function() {
$("#myTopnav").removeClass("responsive");
});
});
ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
ul.topnav li {float: left;}
ul.topnav li a {
display: inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
}
ul.topnav li a:hover {background-color: #555;}
ul.topnav li.icon {display: none;}
@media screen and (max-width:1180px) {
ul.topnav li:not(:first-child) {display: none;}
ul.topnav li.icon {
float: right;
display: inline-block;
}
}
@media screen and (max-width:680px) {
ul.topnav.responsive {position: relative;}
ul.topnav.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
ul.topnav.responsive li {
float: none;
display: inline;
}
ul.topnav.responsive li a {
display: block;
text-align: left;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="topnav" id="myTopnav">
<li class="links"><a href="#home">Home</a></li>
<li class="links"><a href="#news">News</a></li>
<li class="links"><a href="#contact">Contact</a></li>
<li class="links"><a href="#about">About</a></li>
<li class="icon">
<a href="javascript:void(0);" onclick="myFunction()">☰</a>
</li>
</ul>
这篇关于单击后如何关闭此菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!