该页面包含一个侧面菜单栏,菜单栏包含5-6个内容
内容包含子内容
如何将子内容包装到主要内容中,并使子内容不可见,直到有人点击主要内容为止
有可能这样做吗?我有很多子内容,但出于演示目的,我在一个主要内容下只显示了5-6 mqain内容和3-4个子内容。
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}

<style>
body {
font-family: "Lato", sans-serif;
}
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s;
}
.sidenav a:hover {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
</style>
&#13;
<body>
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<div class="user" style="background-image:url(ava.jpg)"></div>
<ul class="nano-content">
<li class="sub-menu">
<a href="javascript:void(0);"><i class="fa fa-cogs"></i><span>Popular Links</span><i class="arrow fa fa-angle-right pull-right"></i></a>
<ul>
<li><a href="ui-alerts-notifications.html">Java Database Connectivity Code</a>
</li>
<li><a href="ui-panels.html">Java Mail Api</a>
</li>
<li><a href="ui-buttons.html">java Itext</a>
</ul>
</li>
</ul>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰</span>
</body>
&#13;
有可能吗?
答案 0 :(得分:3)
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
var hasSubMenu = document.querySelectorAll('.hasSubMenu');
hasSubMenu.forEach(function(i,q){
i.addEventListener('click',function(){
this.classList.toggle('clicked');
})
})
body {
font-family: "Lato", sans-serif;
}
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s;
}
.sidenav a:hover {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
.hasSubMenu + ul{
display: none;
}
.clicked + ul{
display: inline-block;
}
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<div class="user" style="background-image:url(ava.jpg)"></div>
<ul class="nano-content">
<li class="sub-menu">
<a href="javascript:void(0);" class="hasSubMenu" ><i class="fa fa-cogs"></i><span>Popular Links</span><i class="arrow fa fa-angle-right pull-right"></i></a>
<ul>
<li><a href="ui-alerts-notifications.html">Java Database Connectivity Code</a></li>
<li><a href="ui-panels.html">Java Mail Api</a></li>
<li><a href="ui-buttons.html">java Itext</a></li>
</ul>
</li>
</ul>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰</span>
它只是示例代码。
您应该尝试学习如何制作更好的代码。