一、导语
接下来我们来做一个左侧菜单,因为我们这个经常用到,接下来我们来看看这个这个咋做的,废话不多说了,进入实战状态吧。
二、效果图
三、html的代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.hide{
display: none;
}
.item .header{
height: 35px;
background-color: #2459a2;
color: white;
line-height: 35px;
}
</style>
</head>
<body>
<div style="height: 48px;"></div>
<div style="width: 300px;">
<div class="item">
<div >菜单1</div>
<div class="content">
<div>内容1</div>
<div>内容1</div>
<div>内容1</div>
</div>
</div>
<div class="item">
<div >菜单2</div>
<div class="content hide">
<div>内容2</div>
<div>内容2</div>
<div>内容2</div>
</div>
</div>
<div class="item">
<div >菜单3</div>
<div class="content hide">
<div>内容3</div>
<div>内容3</div>
<div>内容3</div>
</div>
</div>
<div class="item">
<div >菜单4</div>
<div class="content hide">
<div>内容4</div>
<div>内容4</div>
<div>内容4</div>
</div>
</div>
</div>
<script>
function ChangeMenu(hid){
//当前菜单标题div
var current_header = document.getElementById(hid);
//获取所有item的数组
var item_list = current_header.parentElement.parentElement.children;
for(var i=0;i<item_list.length;i++){
var current_item = item_list[i];
//把所有的内容全部隐藏
current_item.children[1].classList.add("hide");
}
//把菜单标题下的隐藏去掉
current_header.nextElementSibling.classList.remove("hide");
}
</script>
</body>
</html>