jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。左侧目录树是一种常见的用户界面元素,用于展示层次结构的数据,通常用于文件系统、网站导航或组织结构。
左侧目录树可以分为静态和动态两种类型:
以下是一个简单的 jQuery 实现左侧目录树的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery 左侧目录树</title>
<style>
.tree {
width: 200px;
margin: 0;
padding: 0;
list-style-type: none;
}
.tree li {
margin: 0;
padding: 5px 0 0 20px;
}
.tree li::before {
content: '';
position: absolute;
top: 15px;
left: 0;
height: 1px;
width: 15px;
border-top: 1px solid #ccc;
}
.tree > li::before {
border: 0;
}
.tree li:last-child::after {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 15px;
border-left: 1px solid #ccc;
}
.tree > li:last-child::after {
border-bottom: 0;
}
.tree li > ul {
display: none;
list-style-type: none;
margin: 0;
padding: 0 0 0 20px;
}
.tree li.open > ul {
display: block;
}
</style>
</head>
<body>
<ul class="tree">
<li class="open">目录 1
<ul>
<li>子目录 1.1</li>
<li>子目录 1.2</li>
</ul>
</li>
<li>目录 2
<ul>
<li>子目录 2.1</li>
<li>子目录 2.2</li>
</ul>
</li>
</ul>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('.tree li').click(function() {
$(this).toggleClass('open');
});
});
</script>
</body>
</html>
animate
方法来实现更复杂的动画效果。通过以上示例代码和解决方法,你应该能够实现一个基本的左侧目录树,并解决常见的实现问题。
领取专属 10元无门槛券
手把手带您无忧上云