jquery 无限级下拉菜单的简单实现代码


Posted in Javascript onFebruary 21, 2014

本例子使用json数据,拼接ul和li来实现的
效果图:

jquery 无限级下拉菜单的简单实现代码

1.准备json数据:

var menuData = [
            {id:0,pid:-1,name:"订购产品",url:"",children:[
                {id:1,pid:0,name:"电脑配件",url:"https://3water.com",children:[
                    {id:20,pid:1,name:"cpu",url:"https://3water.com",children:[
                        {id:30,pid:20,name:"Intel",url:"https://3water.com",children:[
                            {id:3000,pid:30,name:"Intel 01",url:""},
                            {id:3001,pid:30,name:"Intel 02",url:""},
                            {id:3002,pid:30,name:"Intel 03",url:""},
                            {id:3003,pid:30,name:"Intel 04",url:""},
                            {id:3004,pid:30,name:"Intel 05",url:""},
                            {id:3005,pid:30,name:"Intel 06",url:""},
                            {id:3006,pid:30,name:"Intel 07",url:""},
                            {id:3007,pid:30,name:"Intel 08",url:""},
                            {id:3008,pid:30,name:"Intel 09",url:""}
                        ]},
                        {id:31,pid:20,name:"AMD",url:"https://3water.com",children:[
                            {id:3100,pid:31,name:"AMD 01",url:""},
                            {id:3101,pid:31,name:"AMD 02",url:""},
                            {id:3102,pid:31,name:"AMD 03",url:""},
                            {id:3103,pid:31,name:"AMD 04",url:""},
                            {id:3104,pid:31,name:"AMD 05",url:""},
                            {id:3105,pid:31,name:"AMD 06",url:""},
                            {id:3106,pid:31,name:"AMD 07",url:""},
                            {id:3107,pid:31,name:"AMD 08",url:""},
                            {id:3108,pid:31,name:"AMD 09",url:""}
                        ]}
                    ]},
                    {id:21,pid:1,name:"内存",url:"https://3water.com"},
                    {id:22,pid:1,name:"硬盘",url:"https://3water.com"},
                    {id:23,pid:1,name:"主板",url:"https://3water.com"},
                    {id:24,pid:1,name:"显卡",url:"https://3water.com"},
                    {id:25,pid:1,name:"显示器",url:"https://3water.com"},
                    {id:26,pid:1,name:"主机箱",url:"https://3water.com"},
                    {id:27,pid:1,name:"主机箱电源",url:"https://3water.com"},
                    {id:28,pid:1,name:"键鼠(有线)",url:"https://3water.com"},
                    {id:29,pid:1,name:"键鼠(无线)",url:"https://3water.com"}
                ],url:"https://3water.com"},
                {id:101,pid:0,name:"监控器材",children:[
                    {id:102,pid:101,name:"摄像头",url:"https://3water.com"},
                    {id:103,pid:101,name:"摄像头",url:"https://3water.com"},
                    {id:104,pid:101,name:"摄像头",url:"https://3water.com"},
                    {id:112,pid:101,name:"摄像头",url:"https://3water.com"},
                    {id:113,pid:101,name:"摄像头",url:"https://3water.com"},
                    {id:114,pid:101,name:"摄像头",url:"https://3water.com"},
                    {id:115,pid:101,name:"摄像头",url:"https://3water.com"},
                    {id:116,pid:101,name:"摄像头",url:"https://3water.com"},
                    {id:117,pid:101,name:"摄像头",url:"https://3water.com"}
                ],url:"https://3water.com"},
                {id:201,pid:0,name:"数码产品",children:[
                    {id:202,pid:201,name:"摄像头",url:"https://3water.com"}
                ],url:"https://3water.com"},
                {id:301,pid:0,name:"网络产品",children:[
                    {id:302,pid:301,name:"摄像头",url:"https://3water.com"}
                ],url:"https://3water.com"},
                {id:401,pid:0,name:"打印耗材",children:[
                    {id:402,pid:401,name:"打印机",url:"https://3water.com"},
                    {id:403,pid:401,name:"油墨",url:"https://3water.com"},
                    {id:404,pid:401,name:"纸张",url:"https://3water.com"},
                    {id:405,pid:401,name:"摄像头",url:"https://3water.com"},
                    {id:406,pid:401,name:"摄像头",url:"https://3water.com"},
                    {id:407,pid:401,name:"摄像头",url:"https://3water.com"},
                    {id:408,pid:401,name:"摄像头",url:"https://3water.com"}
                ],url:"https://3water.com"}
            ]},
            {id:1000,pid:-1,name:"我的订单",url:"",children:[
                {id:1001,pid:1000,name:"已过期订单",url:""},
                {id:1001,pid:1000,name:"已付款订单",url:""}
            ]},
            {id:2000,pid:-1,name:"公司信息",url:"",children:[
                {id:2001,pid:2000,name:"最新新闻",url:""},
                {id:2002,pid:2000,name:"公司地址",url:""}
            ]}
        ];

2.html代码:
<div id="menu"><ul id="baseMenu"></ul></div>

3.解析json数据(plugin-menu.js文件):刚学会写jquery插件,写的还比较乱,凑合着看吧
(function($){
    $.fn.extend({
        menu:function(options){
            var defaults = {
                data:[],
                ulId:"baseMenu"
            };
            var options = $.extend(defaults, options);
            // 开始拼接ul,li
            $.each(options.data,function(i,v){
                var li = $("<li id='"+options.data[i].id+"' name='"+options.data[i].pid+"'></li>");
                var _a = $("<a>"+options.data[i].name+"</a>");
                _a.attr("href",options.data[i].url)
                    .appendTo(li);                _each(options.data[i],li);
                li.appendTo($("#"+options.ulId));
            });
            // 给li添加事件
            $(this).find("li").hover(function(){
                var id = $(this).attr("id");
                $(this).find("ul[name='"+id+"']").show();
            },function(){
                var id = $(this).attr("id");
                $(this).find("ul[name='"+id+"']").hide();
            });
        }
    });
})(jQuery);

因为支持无限级,所以肯定会用到递归方法:
function _each(data,li){
    if(data==undefined||data.children==undefined){
        return false;
    }
    var ul = $("<ul name='"+data.id+"' style='display:none;'></ul>");    $.each(data.children,function(i,v){
        var _li = $("<li id='"+data.children[i].id+"' name='"+data.children[i].pid+"'></li>");
        var _a = $("<a>"+data.children[i].name+"</a>");
        _a.attr("href",data.children[i].url)
            .attr("target","_blank")
            .appendTo(_li);
        if(data.children[i].children!=undefined){
            _each(data.children[i],_li);
        }
        _li.appendTo(ul);
    });
    ul.appendTo(li);
}

4.调用插件:
$(function() {
    $("#menu").menu({data:menuData,ulId:"baseMenu"});
});

最后,css样式:
ul,li{list-style:none;padding:0px;margin:0px;}
#menu *{line-height:30px;}
#menu a{text-decoration:none;}
#menu ul{text-align:left;}
#menu>ul>li{text-align:center;width:80px;float:left;}
#menu>ul>li>a{color:#000;}
#menu>ul>li:hover{background:#F0F0F0;}
#menu>ul>li ul{display:none;width:150px;position:absolute;background:#c1cd94;box-shadow:2px 2px 2px #000;-webkit-box-shadow:2px 2px 2px #000;
-moz-box-shadow:2px 2px 2px #123;}
#menu>ul>li>ul li{padding-left:5px; position:relative;}
#menu>ul>li>ul li>a{color:#000;}
#menu>ul>li>ul li:hover{background:#d3dbb3;}
#menu>ul>li>ul>li ul{left:150px; top:0px;}
Javascript 相关文章推荐
Web层改进II-用xmlhttp 无声息提交复杂表单
Jan 22 Javascript
javascript window对象属性整理
Oct 24 Javascript
FusionCharts图表显示双Y轴双(多)曲线
Nov 22 Javascript
jquery ajax修改全局变量示例代码
Nov 08 Javascript
在linux中使用包管理器安装node.js
Mar 13 Javascript
jquery自定义表格样式
Nov 23 Javascript
解析javascript瀑布流原理实现图片滚动加载
Mar 10 Javascript
jquery取消事件冒泡的三种方法(推荐)
May 28 Javascript
web前端vue实现插值文本和输出原始html
Jan 19 Javascript
element el-input directive数字进行控制
Oct 11 Javascript
详解微信小程序的不同函数调用的几种方法
May 08 Javascript
javascript中导出与导入实现模块化管理教程
Dec 03 Javascript
jQuery 无限级菜单的简单实例
Feb 21 #Javascript
js控制table合并具体实现
Feb 20 #Javascript
js文件Cookie存取值示例代码
Feb 20 #Javascript
js中array的sort()方法使用介绍
Feb 20 #Javascript
javascript生成随机大小写字母的方法
Feb 20 #Javascript
将json对象转换为字符串的方法
Feb 20 #Javascript
javascript实现json页面分页实例代码
Feb 20 #Javascript
You might like
学习php设计模式 php实现策略模式(strategy)
2015/12/07 PHP
反射调用private方法实践(php、java)
2015/12/21 PHP
PHP pthreads v3使用中的一些坑和注意点分析
2020/02/21 PHP
Track Image Loading效果代码分析
2007/08/13 Javascript
javascript 清空form表单中某种元素的值
2009/12/26 Javascript
Windows系统下使用Sublime搭建nodejs环境
2015/04/13 NodeJs
jQuery实现类似淘宝网图片放大效果的方法
2015/07/08 Javascript
Javascript实现的Map集合工具类完整实例
2015/07/31 Javascript
JavaScript+html5 canvas绘制缤纷多彩的三角形效果完整实例
2016/01/26 Javascript
AnjularJS中$scope和$rootScope的区别小结
2016/09/18 Javascript
bootstrap网格系统使用方法解析
2017/01/13 Javascript
jquery dialog获取焦点的方法
2017/02/09 Javascript
vuex state中的数组变化监听实例
2019/11/06 Javascript
支付宝小程序实现省市区三级联动
2020/06/21 Javascript
Python查看多台服务器进程的脚本分享
2014/06/11 Python
pygame学习笔记(2):画点的三种方法和动画实例
2015/04/15 Python
python获取多线程及子线程的返回值
2017/11/15 Python
Python数据可视化之画图
2019/01/15 Python
Python3.6 + TensorFlow 安装配置图文教程(Windows 64 bit)
2020/02/24 Python
简单了解Python变量作用域正确使用方法
2020/06/12 Python
TensorFlow Autodiff自动微分详解
2020/07/06 Python
html5视频常用API接口的实战示例
2020/03/20 HTML / CSS
可持续木材、生态和铝制太阳镜:Proof Eyewear
2019/07/24 全球购物
史上最全面的Java面试题汇总!
2015/02/03 面试题
UNIX文件类型
2013/08/29 面试题
工厂厂长岗位职责
2013/11/08 职场文书
《十六年前的回忆》教学反思
2014/02/14 职场文书
高中军训感言1000字
2014/03/01 职场文书
保护环境标语
2014/06/09 职场文书
争做文明公民倡议书
2014/08/29 职场文书
国际商务专业毕业生自我鉴定2014
2014/09/27 职场文书
大学新生入学感想
2015/08/07 职场文书
干货!开幕词的写作方法
2019/04/02 职场文书
python3 删除所有自定义变量的操作
2021/04/08 Python
JavaScript展开运算符和剩余运算符的区别详解
2022/02/18 Javascript
Golang 1.18 多模块Multi-Module工作区模式的新特性
2022/04/11 Golang