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 相关文章推荐
JS中剪贴板兼容性、判断复制成功或失败
Mar 09 Javascript
jQuery 入门讲解1
Apr 15 Javascript
js 方法实现返回多个数据的代码
Apr 30 Javascript
javascript实现的基于金山词霸网络翻译的代码
Jan 15 Javascript
qTip 基于JQuery的Tooltip插件[兼容性好]
Sep 01 Javascript
使用mouse事件实现简单的鼠标经过特效
Jan 30 Javascript
Javascript中prototype属性实现给内置对象添加新的方法
May 14 Javascript
jQuery实现动态添加和删除一个div
Aug 12 Javascript
JS实现的简洁纵向滑动菜单(滑动门)效果
Oct 19 Javascript
javascript创建对象的3种方法
Nov 02 Javascript
用js制作淘宝放大镜效果
Oct 28 Javascript
JavaScript显式数据类型转换详解
Mar 18 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开发的一些注意点总结
2010/10/12 PHP
PHP页面间传递值和保持值的方法
2016/08/24 PHP
PHP的重载使用魔术方法代码实例详解
2021/02/26 PHP
innerText和innerHTML 一些问题分析
2009/05/18 Javascript
jquery last-child 列表最后一项的样式
2010/01/22 Javascript
javascript之学会吝啬 精简代码
2010/04/25 Javascript
javascript使用activex控件的代码
2011/01/27 Javascript
js判断当页面无法回退时关闭网页否则就history.go(-1)
2014/08/07 Javascript
详解JavaScript的变量和数据类型
2015/11/27 Javascript
Bootstrap导航条鼠标悬停下拉菜单
2017/01/04 Javascript
nodejs开发微信小程序实现密码加密
2017/07/11 NodeJs
详解jQuery同步Ajax带来的UI线程阻塞问题及解决办法
2017/08/09 jQuery
layui使用label标签的方法
2019/09/14 Javascript
JavaScript获取页面元素的常用方法详解
2019/09/28 Javascript
工作中常用js功能汇总
2020/11/07 Javascript
vue3.0自定义指令(drectives)知识点总结
2020/12/27 Vue.js
Python使用微信SDK实现的微信支付功能示例
2017/06/30 Python
对Python3之进程池与回调函数的实例详解
2019/01/22 Python
Python获取网段内ping通IP的方法
2019/01/31 Python
opencv 获取rtsp流媒体视频的实现方法
2019/08/23 Python
python RC4加密操作示例【测试可用】
2019/09/26 Python
python生成器/yield协程/gevent写简单的图片下载器功能示例
2019/10/28 Python
PyCharm+Miniconda3安装配置教程详解
2021/02/16 Python
CSS3 三维变形实现立体方块特效源码
2016/12/15 HTML / CSS
深入剖析webstorage[html5的本地数据处理]
2016/07/11 HTML / CSS
美国网上订购鲜花:FTD
2016/09/23 全球购物
Styleonme中文网:韩国高档人气品牌
2017/06/21 全球购物
金融专业个人的自我评价
2013/10/18 职场文书
镇政府副镇长群众路线专题民主生活会对照检查材料
2014/09/19 职场文书
先进个人事迹材料
2014/12/29 职场文书
婚礼答谢礼品
2015/01/20 职场文书
上学路上观后感
2015/06/16 职场文书
2015年新教师个人工作总结
2015/10/14 职场文书
七年级英语教学反思
2016/02/15 职场文书
七年级作文之雪景
2019/11/18 职场文书
MySQL 如何分析查询性能
2021/05/12 MySQL