<script type="text/javascript">
var level1 = ["Beijing","GuangZhou","ShangHai"];
var level2 = [["ZhaoYang","TianTan","GuGong"],["Tianhe","Panyu"],["PuDong","PuXi"]];
var level3 = [[["TianShan","HuangShan"],["TianTan1","TianTan2"],["GuGong1","GuGong2","GuGong3","GuGong4"]],[["TianHe1","TianHe2"],["PanYu1","PanYu2"]],[["PuDong1","PuDong2"],["PuXi1","PuXi2"]]];
var Levels =
{ fL: null,//用存储各级
SELEct的DOM引用
sL: null,
tL: null,
l1: null,
l2: null,
l3: null,
$: function(id)
{ return (typeof id == "ob
ject") ? id : document.getElementById(id
); },
init: function(fID,sID,tID,l1,l2,l3)
{ thi
s.fL = thi
s.$(fID
); thi
s.sL = thi
s.$(sID
); thi
s.tL = thi
s.$(tID
); thi
s.l1 = l1;
thi
s.l2 = l2;
thi
s.l3 = l3;
thi
s.fL
.option
s.add(new Option("
SELEct",-1)
);//给一级菜单添加一个“
SELEct”标志
for (var i = 0; i < l1.length; i++)
{ var item = new Option(l1[i],i
); thi
s.fL
.option
s.add(item
); }
thi
s.doLev2(
); //调用doLev2函数,处理二级菜单
thi
s.doLev3(
); //调用doLev3函数,处理三级菜单
},
removeSTL: function()
{ //用于删除第二、三级的菜单项
thi
s.sL
.option
s.length = 0;
thi
s.tL
.option
s.length = 0;
},
removeTL: function()
{ //用于删除第三级的菜单项
thi
s.tL
.option
s.length = 0;
},
doLev2: function()
{ //处理二级菜单
var that = this;
thi
s.fL
.onchange = function()
{ that.removeSTL(
); //删除旧second的
SELEct
if (that.f
l.SELEctedIndex == 0)
{ that.removeSTL(
); // //删除旧second的
SELEct
}
else
{ that.sL
.option
s.add(new Option("
SELEct",-1)
); //给二级菜单添加一个“
SELEct”标志
//获取第二级所需的数组
var items = that.l2[that.f
l.SELEctedIndex - 1];
for (var i = 0; i < item
s.length; i++)
{ //添加第二级的新
SELEct项
var item = new Option(items[i],i
); that.sL
.option
s.add(item
); }
}
}
},
doLev3: function()
{ //处理三级菜单
var that = this;
thi
s.sL
.onchange = function()
{ that.removeTL(
); //删除旧third的
SELEct
if (that.s
l.SELEctedIndex == 0)
{ that.removeTL(
); //删除旧third的
SELEct
}
else
{ that.tL
.option
s.add(new Option("
SELEct",-1)
); //给三级菜单添加一个“
SELEct”标志
//获取第三级所需的数组
var items = that.l3[that.f
l.SELEctedIndex - 1][that.s
l.SELEctedIndex - 1];
for (var i = 0; i < item
s.length; i++)
{ //添加第三级的新
SELEct项
var item = new Option(items[i],i
); that.tL
.option
s.add(item
); }
}
}
}
}
onload = function()
{ Level
s.init("first","second","third",level1,level2,level3
); //调用Levels的init方法
}
</script>