使用jquery来实现二层嵌套的左侧列表,点击后显示右侧内容的效果。
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title>
<script src="js/jquery.min.js">script>
<style>
.container {
display: flex;
}
.left-panel {
width: 30%;
background-color: #f2f2f2;
}
.left-list {
list-style: none;
padding: 0;
margin: 0;
}
.list-item {
padding: 10px;
cursor: pointer;
}
.sub-list {
list-style: none;
padding: 0;
margin: 10px 0 0 20px;
}
.sub-item {
padding: 5px;
cursor: pointer;
}
.right-panel {
flex: 1;
background-color: #fff;
}
.content {
display: none;
padding: 20px;
}
style>
head>
<body>
<div class="container">
<div class="left-panel">
<ul class="left-list">
<li class="list-item">
项目1
<ul class="sub-list">
<li class="sub-item" data-target="content1">子项目1li>
<li class="sub-item" data-target="content2">子项目2li>
<li class="sub-item" data-target="content3">子项目3li>
ul>
li>
<li class="list-item">
项目2
<ul class="sub-list">
<li class="sub-item" data-target="content4">子项目4li>
<li class="sub-item" data-target="content5">子项目5li>
<li class="sub-item" data-target="content6">子项目6li>
ul>
li>
<li class="list-item">
项目3
<ul class="sub-list">
<li class="sub-item" data-target="content7">子项目7li>
<li class="sub-item" data-target="content8">子项目8li>
<li class="sub-item" data-target="content9">子项目9li>
ul>
li>
ul>
div>
<div class="right-panel">
<div class="content" id="content1">
1
div>
<div class="content" id="content2">
2
div>
<div class="content" id="content3">
3
div>
<div class="content" id="content4">
4
div>
<div class="content" id="content5">
5
div>
<div class="content" id="content6">
6
div>
<div class="content" id="content7">
7
div>
<div class="content" id="content8">
8
div>
<div class="content" id="content9">
9
div>
div>
div>
<script>
$(document).ready(function() {
$('.list-item').click(function() {
$(this).siblings().find('.sub-list').slideUp();
$(this).find('.sub-list').slideToggle();
});
$('.sub-item').click(function(event) {
event.stopPropagation();
var target = $(this).data('target');
$('.content').hide();
$('#' + target).show();
});
});
script>
body>
html>