
基于jquery的简洁树形折叠菜单
发布日期:2021-05-09 05:52:52
浏览次数:10
分类:博客文章
本文共 2926 字,大约阅读时间需要 9 分钟。
先上效果图:
最小的ul就是一个最小的树枝,空间允许时可无限扩展。html如下:
css如下:
div.panel:first-child { margin-top:20px;}div.treeview { min-width: 100px; min-height: 100px; max-height: 256px; overflow:auto; padding: 4px; margin-bottom: 20px; color: #369; border: solid 1px; border-radius: 4px;}div.treeview ul:first-child:before { display: none;}.treeview, .treeview ul { margin:0; padding:0; list-style:none; color: #369;}.treeview ul { margin-left:1em; position:relative}.treeview ul ul { margin-left:.5em}.treeview ul:before { content:""; display:block; width:0; position:absolute; top:0; left:0; border-left:1px solid; bottom:15px;}.treeview li { margin:0; padding:0 1em; line-height:2em; font-weight:700; position:relative}.treeview ul li:before { content:""; display:block; width:10px; height:0; border-top:1px solid; margin-top:1px; position:absolute; top:1em; left:0}.tree-indicator { margin-right:5px; cursor:pointer;}.treeview li a { text-decoration: none; color:inherit; cursor:pointer;}.treeview li button, .treeview li button:active, .treeview li button:focus { text-decoration: none; color:inherit; border:none; background:transparent; margin:0px 0px 0px 0px; padding:0px 0px 0px 0px; outline: 0;}
js如下:
$.fn.extend({ treeview: function() { return this.each(function() { var tree = $(this); tree.addClass('treeview-tree'); tree.find('li').each(function() { var stick = $(this); }); tree.find('li').has("ul").each(function () { var branch = $(this); //li with children ul branch.prepend(">"); branch.addClass('tree-branch'); branch.on('click', function (e) { if (this == e.target) { var icon = $(this).children('i:first'); $(this).children().children().toggle(); } }) branch.children().children().toggle(); branch.children('.tree-indicator, button, a').click(function(e) { branch.click(); e.preventDefault(); }); }); }); }});//调用$(window).on('load', function () { $('.treeview').each(function () { var tree = $(this); tree.treeview(); })})
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月02日 16时28分03秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
关于计数排序
2021-05-09
使用 C# 9 的records作为强类型ID - 初次使用
2021-05-09
CentOS Too Many Open Files 解决
2021-05-09
移除 DevExpress 的 XtraForm 标题文字阴影
2021-05-09
【进阶之路】并发编程(三)-非阻塞同步机制
2021-05-09
分布式图数据库 Nebula Graph 中的集群快照实践
2021-05-09
Python3获取新浪微博内容乱码问题
2021-05-09
移动开发中单页异步加载所有列表项
2021-05-09
Newbe.Claptrap 框架入门,第一步 —— 开发环境准备
2021-05-09
最大子序列和问题
2021-05-09
【neo4j】文件管理路径、数据备份、创建新数据库、导入数据等操作记录
2021-05-09
Python中字符串前添加r ,b, u, f前缀的含义
2021-05-09
zookeeper安装部署步骤
2021-05-09
Linux I/O 重定向
2021-05-09
Hadoop学习笔记—Yarn
2021-05-09
对象的绑定方法
2021-05-09
__format__
2021-05-09
大白话OSI七层协议
2021-05-09