• jQuery 树型菜单完整代码


     jQuery 树型菜单完整代码:

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8" />
    5. <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    6. <title>jQuery树形菜单title>
    7. <script src="./js/jquery-3.6.0.js">script>
    8. <style type="text/css">
    9. /* 初始化页面 */
    10. * {
    11. margin: 0;
    12. padding: 0;
    13. box-sizing: border-box;
    14. }
    15. /* 设置背景页面 */
    16. body {
    17. width: 100%;
    18. min-height: 100vh;
    19. background-color: #029688;
    20. display: flex;
    21. justify-content: center;
    22. }
    23. /* 设置树形菜单宽度 */
    24. .tree {
    25. width: 60%;
    26. }
    27. /* 设置树形菜单标题 */
    28. .tree h1 {
    29. width: 100%;
    30. height: 60px;
    31. text-align: center;
    32. line-height: 60px;
    33. color: #F7F29B;
    34. font-family: "楷体";
    35. letter-spacing: 3px;
    36. }
    37. /* 设置标题下边的水平线 */
    38. .tree i{
    39. display: block;
    40. width: 100%;
    41. height: 1px;
    42. background-color: #D7DBDB;
    43. position: relative;
    44. }
    45. /* 设置标题水平线中心的菱形 */
    46. .tree i::before{
    47. content: "";
    48. width: 7px;
    49. height: 7px;
    50. transform: rotate(45deg);
    51. background-color: #D7DBDB;
    52. position: absolute;
    53. top: -3px;
    54. left: 50%;
    55. margin-left: -3.5px;
    56. }
    57. /* 设置树形菜单列表 */
    58. .tree-list{
    59. width: 100%;
    60. margin-top: 20px;
    61. }
    62. /* 设置列表缩进效果 */
    63. .tree-list div{
    64. width: 100%;
    65. padding-left: 40px;
    66. position: relative;
    67. display: none;
    68. }
    69. /* 设置列表名称 */
    70. .tree-list p{
    71. display: flex;
    72. justify-content: space-between;
    73. width: 100%;
    74. height: 40px;
    75. line-height: 40px;
    76. color: #F7FBFB;
    77. }
    78. /* 设置箭头图片 */
    79. .tree-list img{
    80. width: 14px;
    81. height: 14px;
    82. margin: 13px;
    83. transition: all 0.3s;
    84. }
    85. /* 设置箭头图片旋转效果 */
    86. .tree-list .active{
    87. transform: rotate(180deg);
    88. }
    89. /* 设置鼠标悬浮样式 */
    90. .tree-list p:hover{
    91. background-color: rgba(80, 220, 220, 0.2);
    92. }
    93. /* 设置列表分类水平线 */
    94. .tree-list>em{
    95. display: block;
    96. width: 100%;
    97. height: 2px;
    98. background-color: #D7DBDB;
    99. margin: 5px 0;
    100. }
    101. style>
    102. head>
    103. <body>
    104. <div class="tree">
    105. <h1>数据分类h1>
    106. <i>i>
    107. <div class="tree-list">div>
    108. div>
    109. <script type="text/javascript">
    110. // 设置变量用于存储拼接的标签字符串
    111. let content = "";
    112. // 请求本地 json 文件
    113. $.ajax({
    114. type:"GET",
    115. url:"./js/data.json",
    116. success:function(res){
    117. console.log(res);
    118. // 调用数据拼接函数
    119. render(res);
    120. // 将字符串输出到页面中
    121. $(".tree-list").html(content)
    122. }
    123. })
    124. // 数据拼接函数 将数据拼接成标签字符串
    125. function render(arr){
    126. for(let item of arr){
    127. if(item.child != undefined && item.child.length > 0){
    128. content += `

      ${item.name}

    129. `;
    130. render(item.child);
    131. content += `
      `;
  • }else{
  • content += `

    ${item.name}

    `
    ;
  • }
  • }
  • }
  • // 列表显示隐藏事件
  • function cut(e){
  • e = e || window.event;
  • let targets = e.target || e.srcElemet;
  • $(targets).next("div").toggle(300);
  • $(targets).find("img").toggleClass("active");
  • }
  • // 列表跳转事件
  • function skip(){
  • location.href = "./index.html";
  • }
  • script>
  • body>
  • html>
  •  JSON 假数据文件:

    1. [
    2. {
    3. "name":"一级菜单1",
    4. "child":[
    5. {
    6. "name":"二级菜单1-1",
    7. "child":[]
    8. },{
    9. "name":"二级菜单1-2",
    10. "child":[
    11. {
    12. "name":"三级菜单1-2-1",
    13. "child":[]
    14. },{
    15. "name":"三级菜单1-2-2",
    16. "child":[]
    17. }
    18. ]
    19. },{
    20. "name":"二级菜单1-3",
    21. "child":[]
    22. },{
    23. "name":"二级菜单1-4",
    24. "child":[]
    25. },{
    26. "name":"二级菜单1-5",
    27. "child":[
    28. {
    29. "name":"三级菜单1-5-1",
    30. "child":[]
    31. },{
    32. "name":"三级菜单1-5-2",
    33. "child":[]
    34. }
    35. ]
    36. }
    37. ]
    38. },{
    39. "name":"一级菜单2",
    40. "child":[
    41. {
    42. "name":"二级菜单2-1",
    43. "child":[
    44. {
    45. "name":"三级菜单2-1-1",
    46. "child":[]
    47. },{
    48. "name":"三级菜单2-1-2",
    49. "child":[]
    50. }
    51. ]
    52. },{
    53. "name":"二级菜单2-2",
    54. "child":[]
    55. },{
    56. "name":"二级菜单2-3",
    57. "child":[]
    58. }
    59. ]
    60. },{
    61. "name":"一级菜单3",
    62. "child":[]
    63. },{
    64. "name":"一级菜单4",
    65. "child":[
    66. {
    67. "name":"二级菜单4-1",
    68. "child":[]
    69. },{
    70. "name":"二级菜单4-2",
    71. "child":[
    72. {
    73. "name":"三级菜单4-2-1",
    74. "child":[]
    75. },{
    76. "name":"三级菜单4-2-2",
    77. "child":[]
    78. },{
    79. "name":"三级菜单4-2-3",
    80. "child":[]
    81. }
    82. ]
    83. },{
    84. "name":"二级菜单4-3",
    85. "child":[]
    86. },{
    87. "name":"二级菜单4-4",
    88. "child":[]
    89. },{
    90. "name":"二级菜单4-5",
    91. "child":[]
    92. }
    93. ]
    94. },{
    95. "name":"一级菜单5",
    96. "child":[]
    97. },{
    98. "name":"一级菜单6",
    99. "child":[
    100. {
    101. "name":"二级菜单6-1",
    102. "child":[
    103. {
    104. "name":"三级菜单6-1-1",
    105. "child":[]
    106. },{
    107. "name":"三级菜单6-1-2",
    108. "child":[]
    109. },{
    110. "name":"三级菜单6-1-3",
    111. "child":[]
    112. }
    113. ]
    114. },{
    115. "name":"二级菜单6-2",
    116. "child":[]
    117. },{
    118. "name":"二级菜单6-3",
    119. "child":[]
    120. }
    121. ]
    122. },{
    123. "name":"一级菜单7",
    124. "child":[
    125. {
    126. "name":"二级菜单7-1",
    127. "child":[]
    128. },{
    129. "name":"二级菜单7-2",
    130. "child":[]
    131. }
    132. ]
    133. },{
    134. "name":"一级菜单8",
    135. "child":[]
    136. },{
    137. "name":"一级菜单9",
    138. "child":[]
    139. }
    140. ]

    原创作者:吴小糖

    创作时间:2023.11.7

  • 相关阅读:
    HELM FLOW CONTROL practical operation
    如何为谷歌seo打好基础?
    【Spring】IoC容器 控制反转 与 DI依赖注入 XML实现版本 第二期
    基于Open3D的点云处理22-非阻塞可视化/动态可视化
    LeetCode【69. x 的平方根】
    知识图谱-KGE-对抗模型-2018:KBGAN
    05_利用神经网实现MINST手写数字识别
    python经典百题之分数评级
    2024HW --->蓝队面试题
    行业资讯 | 入门revit软件需要理清哪些概念。
  • 原文地址:https://blog.csdn.net/xiaowude_boke/article/details/134472100