javascript实现无限级select联动菜单


Posted in Javascript onJanuary 02, 2015

代码很简单,这里主要是给大家推荐一下本代码的实现思路,别具一格。

奉上代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>Selects</title>

<style type="text/css">

*{font-size:14px;}

select{height:20px;font-size:12px;}

</style> 

</head>

<body >

<div style='color:red'>2级联动</div>

<div id='demo1'></div><br><br><br>

<div style='color:red'>3级联动</div>

<div id='demo2'></div><br><br><br>

<div style='color:red'>4级联动</div>

<div id='demo3'></div><br><br><br>

<div style='color:red'>5级联动</div>

<div id='demo4'></div><br><br><br>

<script language="javascript">

var Sys = (function(ua){

  var s = {};

  s.IE = ua.match(/msie ([\d.]+)/)?true:false;

  s.Firefox = ua.match(/firefox\/([\d.]+)/)?true:false;

  s.Chrome = ua.match(/chrome\/([\d.]+)/)?true:false;

  s.IE6 = (s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6))?true:false;

  s.IE7 = (s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 7))?true:false;

  s.IE8 = (s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 8))?true:false;

  return s;

})(navigator.userAgent.toLowerCase());

Sys.IE6&&document.execCommand("BackgroundImageCache", false, true); 

function $(Id){

  return document.getElementById(Id);

};

function $$(p,e){

  return p.getElementsByTagName(e);

};

function addListener(element,e,fn){

  element.addEventListener?element.addEventListener(e,fn,false):element.attachEvent("on" + e,fn);

};

function removeListener(element,e,fn){

  element.removeEventListener?element.removeEventListener(e,fn,false):element.detachEvent("on" + e,fn);

};

var Bind = function(object, fun) {

  var args = Array.prototype.slice.call(arguments).slice(2);

  return function() {

    return fun.apply(object, args);

  };

};

var BindAsEventListener = function(object, fun) {

  var args = Array.prototype.slice.call(arguments).slice(2);

  return function(event) {

    return fun.apply(object, [event || window.event].concat(args));

  };

};

var Extend = function(destination, source){

  for (var property in source) {

    destination[property] = source[property];

  };

};

var Class = function(properties){

  var _class = function(){return (arguments[0] !== null && this.initialize && typeof(this.initialize) == 'function') ? this.initialize.apply(this, arguments) : this;};

  _class.prototype = properties;

  return _class;

};
//==========================================================================================================================

var Selects = new Class({

 initialize :function(container,data,title){

  this.container  = container;

  this.num = title.length;

  this.Events = new Array(title.length-1);

  var i,l,select;

  for(i=0; i<this.num ; i++)

  {

   container.innerHTML = container.innerHTML + " " + title[i];

   container.appendChild(document.createElement('select'));

  }

  select = $$(container,'select')[0];

  for(i=0,l=data.length;i<l;i++)

   select.options.add(new Option(data[i].txt,i)); 

  addListener(select,'change',Bind(this,this.Change,select,data,0));

  this.Change(select,data,0);

 },

 Change : function(obj,data,num){

  if(num == this.num-1)return;  

  var menu = data[obj.value].menu;

  select = $$(this.container,'select')[num+1];

  select.length = 0;  

  if(!menu)return;

  if(this.Events[num]!=undefined)removeListener(select,'change',this.Events[num])

  this.Events[num] = Bind(this,this.Change,select,menu,num+1)

  addListener(select,'change',this.Events[num]);  

  for(var i=0,l=menu.length;i<l;i++)

   select.options.add(new Option(menu[i].txt,i));

  this.Change(select,menu,num+1); 

 }

});
//==========================================================================================================================

window.onload = function(){

 var data=[{"txt":"人族","menu":[{"txt":"大法师","menu":[{"txt":"大法技能","menu":[{"txt":"水元素","menu":[{"txt":"召唤物"},{"txt":"穿刺攻击"},{"txt":"很好东西"}]},{"txt":"暴风雪","menu":[{"txt":"魔法攻击"},{"txt":"面积伤害"},{"txt":"很漂亮"},{"txt":"可被打断"}]},{"txt":"辉煌光环"},{"txt":"瞬间移动"}]},{"txt":"英雄说明","menu":[{"txt":"智力英雄"},{"txt":"强大光环"},{"txt":"辅助型"},]}]},{"txt":"山丘之王","menu":[{"txt":"山丘技能","menu":[{"txt":"风暴之锤"},{"txt":"锥地"},{"txt":"锥晕"},{"txt":"天神下凡"}]},{"txt":"英雄说明","menu":[{"txt":"力量英雄"},{"txt":"秒杀能力"},{"txt":"个子很矮"},{"txt":"大技变态"}]}]},{"txt":"圣骑士","menu":[{"txt":"圣骑技能","menu":[{"txt":"光"},{"txt":"无敌"},{"txt":"光环"},{"txt":"复活"}]},{"txt":"英雄说明","menu":[{"txt":"力量英雄"},{"txt":"辅助英雄"},{"txt":"人称奶妈"}]}]},{"txt":"血法师","menu":[{"txt":"血法技能","menu":[{"txt":"火焰"},{"txt":"吸蓝"},{"txt":"虚无"},{"txt":"神鸟凤凰"}]},{"txt":"英雄说明","menu":[{"txt":"智力英雄"},{"txt":"辅助英雄"},{"txt":"长的很帅"}]}]}]},{"txt":"兽族","menu":[{"txt":"贱圣","menu":[{"txt":"贱圣技能","menu":[{"txt":"疾风步"},{"txt":"影分身"},{"txt":"致命一击"},{"txt":"剑刃风暴"}]},{"txt":"英雄说明","menu":[{"txt":"敏捷英雄"},{"txt":"高攻英雄"},{"txt":"杀人越货"},{"txt":"猥亵无敌"},{"txt":"非常强大"}]}]},{"txt":"先知男","menu":[{"txt":"先知技能","menu":[{"txt":"狼"},{"txt":"日"},{"txt":"闪电链"},{"txt":"地震"}]},{"txt":"英雄说明","menu":[{"txt":"智力英雄"},{"txt":"骚扰英雄"}]}]},{"txt":"牛头人酋长","menu":[{"txt":"牛头技能","menu":[{"txt":"冲击波"},{"txt":"阵地"},{"txt":"耐久光环"},{"txt":"复活"}]},{"txt":"英雄说明","menu":[{"txt":"力量英雄"},{"txt":"魔法强大"},{"txt":"长的威猛"},{"txt":"一个肉盾"}]}]},{"txt":"小YY","menu":[{"txt":"小Y技能","menu":[{"txt":"变动物"},{"txt":"治疗波"},{"txt":"小蛇棒子"},{"txt":"全体无敌"}]},{"txt":"英雄说明","menu":[{"txt":"敏捷英雄"},{"txt":"魔法强大"},{"txt":"辅助英雄"}]}]}]},{"txt":"不死族","menu":[{"txt":"死亡骑士","menu":[{"txt":"亡骑技能","menu":[{txt:"大便一拓"},{txt:"邪恶光环"},{txt:"死亡契约"},{txt:"复活亡灵"}]},{"txt":"英雄说明","menu":[{txt:"骑的是羊"},{txt:"未老先衰"},{txt:"冰霜之哀"},{txt:"啊而塞思"}]}]},{"txt":"巫妖","menu":[{"txt":"巫妖技能","menu":[{txt:"暴冰"},{txt:"冰甲"},{txt:"契约"},{txt:"死亡凋零"}]},{"txt":"英雄说明","menu":[{txt:"穿裙子"},{txt:"全是骨头"},{txt:"不男不女"}]}]},{"txt":"恐惧魔王","menu":[{"txt":"魔王技能"},{"txt":"英雄说明"}]},{"txt":"小强王子","menu":[{"txt":"王子技能"},{"txt":"英雄说明"}]}]}];

 new Selects($('demo1'),data,["种族","英雄"]);

 new Selects($('demo2'),data,["种族","英雄","介绍"]);

 new Selects($('demo3'),data,["种族","英雄","介绍","技能"]);

 new Selects($('demo4'),data,["种族","英雄","介绍","技能","技能说明"]);

}

</script>

</body>

</html>

Javascript 相关文章推荐
JS清除IE浏览器缓存的方法
Jul 26 Javascript
jquery及原生js获取select下拉框选中的值示例
Oct 25 Javascript
javascript禁用Tab键脚本实例
Nov 22 Javascript
jquery文档操作wrap()方法实例简述
Jan 10 Javascript
js实现具有高亮显示效果的多级菜单代码
Sep 01 Javascript
使用jquery+CSS3实现仿windows10开始菜单的下拉导航菜单特效
Sep 24 Javascript
Bootstrap导航条学习使用(一)
Feb 08 Javascript
jQuery用noConflict代替$的实现方法
Apr 12 jQuery
JavaScript原型继承_动力节点Java学院整理
Jun 30 Javascript
vue element动态渲染、移除表单并添加验证的实现
Jan 16 Javascript
Vue的props父传子的示例代码
May 20 Javascript
深入理解Vue的数据响应式
May 15 Vue.js
javascript实现表格排序 编辑 拖拽 缩放
Jan 02 #Javascript
原生javascript实现DIV拖拽并计算重复面积
Jan 02 #Javascript
javascript使用smipleChart实现简单图表
Jan 02 #Javascript
原生javascript实现简单的datagrid数据表格
Jan 02 #Javascript
浅谈jQuery事件绑定原理
Jan 02 #Javascript
js+jquery实现图片裁剪功能
Jan 02 #Javascript
javascript 构造函数方式定义对象
Jan 02 #Javascript
You might like
PHP中SESSION使用中的一点经验总结
2012/03/30 PHP
PHP和JavaScrip分别获取关联数组的键值示例代码
2013/09/16 PHP
php工具型代码之印章抠图
2018/07/18 PHP
thinkPHP框架RBAC实现原理分析
2019/02/01 PHP
微信公众号实现扫码获取微信用户信息(网页授权)
2019/04/09 PHP
浅谈Laravel POST,PUT,PATCH 路由的区别
2019/10/15 PHP
javascript获得服务器端控件的ID的实现代码
2011/12/28 Javascript
jquery中交替点击事件的实现代码
2014/02/14 Javascript
js仿苹果iwatch外观的计时器代码分享
2015/08/26 Javascript
layer实现弹窗提交信息
2016/12/12 Javascript
一句jQuery代码实现返回顶部效果(简单实用)
2016/12/28 Javascript
微信小程序之发送短信倒计时功能
2017/08/30 Javascript
打造通用的匀速运动框架(实例讲解)
2017/10/17 Javascript
JavaScript实现字符串与HTML格式相互转换
2020/03/17 Javascript
基于js实现判断浏览器类型代码实例
2020/07/17 Javascript
js回到页面指定位置的三种方式
2020/12/17 Javascript
python实现在控制台输入密码不显示的方法
2015/07/02 Python
Python制作Windows系统服务
2017/03/25 Python
Python银行系统实战源码
2019/10/25 Python
pycharm导入源码的具体步骤
2020/08/04 Python
python如何随机生成高强度密码
2020/08/19 Python
Dune London官网:英国著名奢华鞋履品牌
2017/11/30 全球购物
英国领先的独立时装店:Van Mildert
2019/10/28 全球购物
short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
2014/09/26 面试题
工程造价与管理专业应届生求职信
2013/11/23 职场文书
家长会学生家长演讲稿
2013/12/29 职场文书
创建服务型党组织实施方案
2014/02/25 职场文书
报关员个人职业生涯规划书
2014/03/12 职场文书
初中新生军训方案
2014/05/13 职场文书
门面房租房协议书
2014/08/20 职场文书
旅游活动总结
2014/08/27 职场文书
2014年师德师风自我剖析材料
2014/09/27 职场文书
向国旗敬礼学生寄语大全
2014/09/30 职场文书
大学生实习推荐信
2015/03/27 职场文书
医院感染管理制度
2015/08/05 职场文书
JS不要再到处使用绝对等于运算符了
2021/04/30 Javascript