当用户点击页面右上角的菜单栏<img>
时,<ul>
显示属性应从none
更改为display: block
,反之亦然。我有图像的全局变量(监听点击)和ul(选择显示属性)。
var menuButton = document.getElementById("menuicon");
var menu = document.getElementById("navlink");
function menuclick(){
if (menu.style.display == none){
menu.style.display = "block";
}
else {
menu.style.display = "none";
}
}
menuButton.addEventListener("click", menuclick);
&#13;
body {
background-color: rgb(40, 40, 40);
font-size: 16px;
margin: 0 auto;
font-family: helvetica, arial;}
header {
position: relative;
background: linear-gradient(rgb(40, 40, 140), rgb(50,50,180));
min-height: 40px;
box-shadow: 0px 0px 5px black inset;}
header h1 {
position: relative;
padding: 10px;
font-style: italic;
font-size: 1.2em;
font-weight: bold;
color: white;}
#h1s {
font-size: .6em;}
img {
position: absolute;
top: 0px;
right: 10px;}
header ul {
position: relative;
display: none;}
header ul li {
background-color: red;
text-align: center;
height: 20px;
background-color: grey;
box-shadow: 0px 0px 1px black inset;}
header ul li a {
text-decoration: none;
color: white;
line-height: 20px;
font-size: .6em;}
&#13;
<div id="page">
<header>
<h1> Cole Pratt <span id="h1s"> No.2968615 </span> </h1>
<img src="images/menuicon.png" alt="menuicon" width="40" id="menuicon"/>
<ul id="navlink">
<li><a href="#">home</a></li>
<li><a href="#">about</a></li>
<li><a href="#">bio</a></li>
</ul>
</header>
</div>
&#13;
答案 0 :(得分:0)
您可以使用 setAttribute()
为您完成工作。并且您忘记将 'none'
放在单引号中。
var menuButton = document.getElementById("menuicon");
var menu = document.getElementById("navlink");
function menuclick() {
if (menu.style.display === 'none') {
menu.setAttribute("style", "display:block");
} else {
menu.setAttribute("style", "display:none");
}
}
menuButton.addEventListener("click", menuclick);
答案 1 :(得分:0)
我建议您使用 css class
来显示/隐藏菜单项,而不是使用 inline styles
。见Element.classList.toggle:
var menuButton = document.getElementById("menuicon");
var menu = document.getElementById("navlink");
function menuclick() {
menu.classList.toggle("active");
}
menuButton.addEventListener("click", menuclick);
body {
background-color: rgb(40, 40, 40);
font-size: 16px;
margin: 0 auto;
font-family: helvetica, arial;
}
header {
position: relative;
background: linear-gradient(rgb(40, 40, 140), rgb(50, 50, 180));
min-height: 40px;
box-shadow: 0px 0px 5px black inset;
}
header h1 {
position: relative;
padding: 10px;
font-style: italic;
font-size: 1.2em;
font-weight: bold;
color: white;
}
#h1s {
font-size: .6em;
}
img {
position: absolute;
top: 0px;
right: 10px;
}
header ul {
position: relative;
display: none;
}
header ul.active {
display: block;
}
header ul li {
background-color: red;
text-align: center;
height: 20px;
background-color: grey;
box-shadow: 0px 0px 1px black inset;
}
header ul li a {
text-decoration: none;
color: white;
line-height: 20px;
font-size: .6em;
}
<div id="page">
<header>
<h1> Cole Pratt <span id="h1s"> No.2968615 </span> </h1>
<img src="images/menuicon.png" alt="menuicon" width="40" id="menuicon" />
<ul id="navlink">
<li><a href="#">home</a></li>
<li><a href="#">about</a></li>
<li><a href="#">bio</a></li>
</ul>
</header>
</div>