相思资源网 Design By www.200059.com
本例子使用json数据,拼接ul和li来实现的
效果图:
1.准备json数据:
复制代码 代码如下:
var menuData = [
{id:0,pid:-1,name:"订购产品",url:"",children:[
{id:1,pid:0,name:"电脑配件",url:"https://www.jb51.net",children:[
{id:20,pid:1,name:"cpu",url:"https://www.jb51.net",children:[
{id:30,pid:20,name:"Intel",url:"https://www.jb51.net",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://www.jb51.net",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://www.jb51.net"},
{id:22,pid:1,name:"硬盘",url:"https://www.jb51.net"},
{id:23,pid:1,name:"主板",url:"https://www.jb51.net"},
{id:24,pid:1,name:"显卡",url:"https://www.jb51.net"},
{id:25,pid:1,name:"显示器",url:"https://www.jb51.net"},
{id:26,pid:1,name:"主机箱",url:"https://www.jb51.net"},
{id:27,pid:1,name:"主机箱电源",url:"https://www.jb51.net"},
{id:28,pid:1,name:"键鼠(有线)",url:"https://www.jb51.net"},
{id:29,pid:1,name:"键鼠(无线)",url:"https://www.jb51.net"}
],url:"https://www.jb51.net"},
{id:101,pid:0,name:"监控器材",children:[
{id:102,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:103,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:104,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:112,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:113,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:114,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:115,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:116,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:117,pid:101,name:"摄像头",url:"https://www.jb51.net"}
],url:"https://www.jb51.net"},
{id:201,pid:0,name:"数码产品",children:[
{id:202,pid:201,name:"摄像头",url:"https://www.jb51.net"}
],url:"https://www.jb51.net"},
{id:301,pid:0,name:"网络产品",children:[
{id:302,pid:301,name:"摄像头",url:"https://www.jb51.net"}
],url:"https://www.jb51.net"},
{id:401,pid:0,name:"打印耗材",children:[
{id:402,pid:401,name:"打印机",url:"https://www.jb51.net"},
{id:403,pid:401,name:"油墨",url:"https://www.jb51.net"},
{id:404,pid:401,name:"纸张",url:"https://www.jb51.net"},
{id:405,pid:401,name:"摄像头",url:"https://www.jb51.net"},
{id:406,pid:401,name:"摄像头",url:"https://www.jb51.net"},
{id:407,pid:401,name:"摄像头",url:"https://www.jb51.net"},
{id:408,pid:401,name:"摄像头",url:"https://www.jb51.net"}
],url:"https://www.jb51.net"}
]},
{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;}
效果图:
1.准备json数据:
复制代码 代码如下:
var menuData = [
{id:0,pid:-1,name:"订购产品",url:"",children:[
{id:1,pid:0,name:"电脑配件",url:"https://www.jb51.net",children:[
{id:20,pid:1,name:"cpu",url:"https://www.jb51.net",children:[
{id:30,pid:20,name:"Intel",url:"https://www.jb51.net",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://www.jb51.net",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://www.jb51.net"},
{id:22,pid:1,name:"硬盘",url:"https://www.jb51.net"},
{id:23,pid:1,name:"主板",url:"https://www.jb51.net"},
{id:24,pid:1,name:"显卡",url:"https://www.jb51.net"},
{id:25,pid:1,name:"显示器",url:"https://www.jb51.net"},
{id:26,pid:1,name:"主机箱",url:"https://www.jb51.net"},
{id:27,pid:1,name:"主机箱电源",url:"https://www.jb51.net"},
{id:28,pid:1,name:"键鼠(有线)",url:"https://www.jb51.net"},
{id:29,pid:1,name:"键鼠(无线)",url:"https://www.jb51.net"}
],url:"https://www.jb51.net"},
{id:101,pid:0,name:"监控器材",children:[
{id:102,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:103,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:104,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:112,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:113,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:114,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:115,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:116,pid:101,name:"摄像头",url:"https://www.jb51.net"},
{id:117,pid:101,name:"摄像头",url:"https://www.jb51.net"}
],url:"https://www.jb51.net"},
{id:201,pid:0,name:"数码产品",children:[
{id:202,pid:201,name:"摄像头",url:"https://www.jb51.net"}
],url:"https://www.jb51.net"},
{id:301,pid:0,name:"网络产品",children:[
{id:302,pid:301,name:"摄像头",url:"https://www.jb51.net"}
],url:"https://www.jb51.net"},
{id:401,pid:0,name:"打印耗材",children:[
{id:402,pid:401,name:"打印机",url:"https://www.jb51.net"},
{id:403,pid:401,name:"油墨",url:"https://www.jb51.net"},
{id:404,pid:401,name:"纸张",url:"https://www.jb51.net"},
{id:405,pid:401,name:"摄像头",url:"https://www.jb51.net"},
{id:406,pid:401,name:"摄像头",url:"https://www.jb51.net"},
{id:407,pid:401,name:"摄像头",url:"https://www.jb51.net"},
{id:408,pid:401,name:"摄像头",url:"https://www.jb51.net"}
],url:"https://www.jb51.net"}
]},
{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;}
标签:
jquery,下拉菜单
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无jquery 无限级下拉菜单的简单实现代码的评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?