javascript实现无限级select联动菜单


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<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>2级联动</div>
<div></div><br><br><br>
<div>3级联动</div>
<div></div><br><br><br>
<div>4级联动</div>
<div></div><br><br><br>
<div>5级联动</div>
<div></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); 
 }
});

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgxdyj.html