我是网络开发的新手,我没有太多经验。我一直在尝试创建一个菜单,当用户点击它时会显示。我已经能够弹出它但它不会保持可见。它会在一秒钟内变得可见,并再次变为不可见。我也试过'display'属性。但是,这有相同的结果。这是我的代码:
<head>
<script type="text/javascript">
function opnmenu () {
document.getElementById("menu").style.visibility="visible";
}
</script>
</head>
<body onLoad="document.getElementById('menu').style.visibility='hidden';">
<div id="menubar">
<table>
<tr>
<td>
<div id="menuimg">
<form>
<input type="image" class="menubut" src="menu.png" alt="submit" onClick="opnmenu()">
</form>
</div>
</td>
</tr>
</table>
</div>
<br>
<h1> Entrepreneur</h1>
<hr>
<div id="menu">
<ul>
<li>Home</li>
<li>Motivation</li>
<li>Books</li>
<li>Videos</li>
</ul>
</div>
</body>
任何帮助将不胜感激。 谢谢。
答案 0 :(得分:2)
以下是使用纯javascript(某些代码来自其他评论)的示例,其中包含更合适的HTML5:
http://jsfiddle.net/59su4/1/
visibility: hidden
的问题在于,如果菜单可见,菜单将占用通常所需的位置。以下是visibility
属性的示例:
http://jsfiddle.net/59su4/2/
(注意隐藏菜单占用的空白区域)
[编辑] 在作者评论之后,我将HTML,JS和CSS混合到一个代码中,以显示每个元素在页面中的位置。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>My menu is ALIVEEEE</title>
<style>
.menu {
display: none;
}
.menu.opened {
display: block;
}
</style>
</head>
<body>
<a href="" class="open-menu">Toggle menu</a>
<nav class="menu">
<ul>
<li>Home</li>
<li>Motivation</li>
<li>Books</li>
<li>Videos</li>
</ul>
</nav>
<p>
To facilitate the process, AI Lab hackers had built a system that displayed both the "source" and "display" modes on a split screen. Despite this innovative hack, switching from mode to mode was still a nuisance.
</p>
<script>
var openMenuBtn = document.getElementsByClassName('open-menu')[0],
menu = document.getElementsByClassName('menu')[0];
openMenuBtn.addEventListener('click', function ( e ) {
// We don't want the empty link to be followed
e.preventDefault();
// Toggle the menu
menu.classList.toggle('opened');
}, false);
</script>
</body>
</html>