javascript 三級(jí)下拉選擇菜單Levels對(duì)象
更新時(shí)間:2009年07月21日 22:59:30 作者:
javascript 三級(jí)下拉選擇菜單Levels對(duì)象,非常的不錯(cuò),大家可以參考下。
JavaScript:
<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,//用存儲(chǔ)各級(jí)select的DOM引用
sL: null,
tL: null,
l1: null,
l2: null,
l3: null,
$: function(id){
return (typeof id == "object") ? id : document.getElementById(id);
},
init: function(fID, sID, tID, l1, l2, l3){
this.fL = this.$(fID);
this.sL = this.$(sID);
this.tL = this.$(tID);
this.l1 = l1;
this.l2 = l2;
this.l3 = l3;
this.fL.options.add(new Option("Select",-1));//給一級(jí)菜單添加一個(gè)“select”標(biāo)志
for (var i = 0; i < l1.length; i++) {
var item = new Option(l1[i], i);
this.fL.options.add(item);
}
this.doLev2(); //調(diào)用doLev2函數(shù),處理二級(jí)菜單
this.doLev3(); //調(diào)用doLev3函數(shù),處理三級(jí)菜單
},
removeSTL: function(){ //用于刪除第二、三級(jí)的菜單項(xiàng)
this.sL.options.length = 0;
this.tL.options.length = 0;
},
removeTL: function(){ //用于刪除第三級(jí)的菜單項(xiàng)
this.tL.options.length = 0;
},
doLev2: function(){ //處理二級(jí)菜單
var that = this;
this.fL.onchange = function(){
that.removeSTL(); //刪除舊second的select
if (that.fL.selectedIndex == 0) {
that.removeSTL(); // //刪除舊second的select
}
else {
that.sL.options.add(new Option("Select", -1)); //給二級(jí)菜單添加一個(gè)“select”標(biāo)志
//獲取第二級(jí)所需的數(shù)組
var items = that.l2[that.fL.selectedIndex - 1];
for (var i = 0; i < items.length; i++) { //添加第二級(jí)的新select項(xiàng)
var item = new Option(items[i], i);
that.sL.options.add(item);
}
}
}
},
doLev3: function(){ //處理三級(jí)菜單
var that = this;
this.sL.onchange = function(){
that.removeTL(); //刪除舊third的select
if (that.sL.selectedIndex == 0) {
that.removeTL(); //刪除舊third的select
}
else {
that.tL.options.add(new Option("Select", -1)); //給三級(jí)菜單添加一個(gè)“select”標(biāo)志
//獲取第三級(jí)所需的數(shù)組
var items = that.l3[that.fL.selectedIndex - 1][that.sL.selectedIndex - 1];
for (var i = 0; i < items.length; i++) { //添加第三級(jí)的新select項(xiàng)
var item = new Option(items[i], i);
that.tL.options.add(item);
}
}
}
}
}
onload = function(){
Levels.init("first", "second", "third", level1,level2,level3); //調(diào)用Levels的init方法
}
</script>
HTML:
<form>
<select id="first">
</select>
<select id="second">
</select>
<select id="third">
</select>
</form>
演示代碼:
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
復(fù)制代碼 代碼如下:
<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,//用存儲(chǔ)各級(jí)select的DOM引用
sL: null,
tL: null,
l1: null,
l2: null,
l3: null,
$: function(id){
return (typeof id == "object") ? id : document.getElementById(id);
},
init: function(fID, sID, tID, l1, l2, l3){
this.fL = this.$(fID);
this.sL = this.$(sID);
this.tL = this.$(tID);
this.l1 = l1;
this.l2 = l2;
this.l3 = l3;
this.fL.options.add(new Option("Select",-1));//給一級(jí)菜單添加一個(gè)“select”標(biāo)志
for (var i = 0; i < l1.length; i++) {
var item = new Option(l1[i], i);
this.fL.options.add(item);
}
this.doLev2(); //調(diào)用doLev2函數(shù),處理二級(jí)菜單
this.doLev3(); //調(diào)用doLev3函數(shù),處理三級(jí)菜單
},
removeSTL: function(){ //用于刪除第二、三級(jí)的菜單項(xiàng)
this.sL.options.length = 0;
this.tL.options.length = 0;
},
removeTL: function(){ //用于刪除第三級(jí)的菜單項(xiàng)
this.tL.options.length = 0;
},
doLev2: function(){ //處理二級(jí)菜單
var that = this;
this.fL.onchange = function(){
that.removeSTL(); //刪除舊second的select
if (that.fL.selectedIndex == 0) {
that.removeSTL(); // //刪除舊second的select
}
else {
that.sL.options.add(new Option("Select", -1)); //給二級(jí)菜單添加一個(gè)“select”標(biāo)志
//獲取第二級(jí)所需的數(shù)組
var items = that.l2[that.fL.selectedIndex - 1];
for (var i = 0; i < items.length; i++) { //添加第二級(jí)的新select項(xiàng)
var item = new Option(items[i], i);
that.sL.options.add(item);
}
}
}
},
doLev3: function(){ //處理三級(jí)菜單
var that = this;
this.sL.onchange = function(){
that.removeTL(); //刪除舊third的select
if (that.sL.selectedIndex == 0) {
that.removeTL(); //刪除舊third的select
}
else {
that.tL.options.add(new Option("Select", -1)); //給三級(jí)菜單添加一個(gè)“select”標(biāo)志
//獲取第三級(jí)所需的數(shù)組
var items = that.l3[that.fL.selectedIndex - 1][that.sL.selectedIndex - 1];
for (var i = 0; i < items.length; i++) { //添加第三級(jí)的新select項(xiàng)
var item = new Option(items[i], i);
that.tL.options.add(item);
}
}
}
}
}
onload = function(){
Levels.init("first", "second", "third", level1,level2,level3); //調(diào)用Levels的init方法
}
</script>
HTML:
復(fù)制代碼 代碼如下:
<form>
<select id="first">
</select>
<select id="second">
</select>
<select id="third">
</select>
</form>
演示代碼:
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
您可能感興趣的文章:
- vuejs實(shí)現(xiàn)下拉框菜單選擇
- JS實(shí)現(xiàn)點(diǎn)擊下拉菜單把選擇的內(nèi)容同步到input輸入框內(nèi)的實(shí)例
- javascript實(shí)現(xiàn)日期三級(jí)聯(lián)動(dòng)下拉框選擇菜單
- 使用Javascript實(shí)現(xiàn)選擇下拉菜單互移并排序
- 基于javascript實(shí)現(xiàn)全國省市二級(jí)聯(lián)動(dòng)下拉選擇菜單
- js模擬淘寶網(wǎng)的多級(jí)選擇菜單實(shí)現(xiàn)方法
- js實(shí)現(xiàn)的全國省市二級(jí)聯(lián)動(dòng)下拉選擇菜單完整實(shí)例
- js選擇并轉(zhuǎn)移導(dǎo)航菜單示例代碼
- 純JSP+DWR實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)下拉選擇菜單實(shí)現(xiàn)技巧
- js實(shí)現(xiàn)圓形菜單選擇器
相關(guān)文章
騰訊QQ網(wǎng)頁在線客服,隨網(wǎng)頁滾動(dòng)條上下移動(dòng)的效果一
騰訊QQ網(wǎng)頁在線客服,隨網(wǎng)頁滾動(dòng)條上下移動(dòng)的效果一...2007-03-03
用js實(shí)現(xiàn)的比較經(jīng)典實(shí)用的觸發(fā)型導(dǎo)航菜單
用js實(shí)現(xiàn)的比較經(jīng)典實(shí)用的觸發(fā)型導(dǎo)航菜單...2007-03-03
Div+Js實(shí)現(xiàn)的帶陰影菜單 微軟以前網(wǎng)站曾用過
Div+Js實(shí)現(xiàn)的陰影菜單,微軟以前老版網(wǎng)站首頁曾用過,用以索引出Windows產(chǎn)品家族的所有產(chǎn)品,本菜單僅實(shí)現(xiàn)了一個(gè)雛形,想實(shí)現(xiàn)像微軟一樣的效果,估計(jì)還需要做不少工作。放出來的目的是供學(xué)習(xí)CSS的朋友做參考。2009-12-12
基于jquery的slideDown和slideUp做手風(fēng)琴
slideDown和slideUp做手風(fēng)琴菜單面板的代碼,也就是大家常說的折疊/展開菜單,不少朋友都喜歡的風(fēng)格,本代碼兼容性好,簡單易懂,便于修改,希望大家喜歡。2011-02-02
CSS鼠標(biāo)懸停菜單 圖片交換技術(shù)實(shí)現(xiàn)
這是一個(gè)使用了圖片交換技術(shù)制作的CSS鼠標(biāo)懸停菜單,也就是大家所說的鼠標(biāo)經(jīng)過換色,不過本例中的菜單并沒有使用純CSS來實(shí)現(xiàn)背景換色,這里將用一幅圖片替換了背景色,因?yàn)橹饕窍胱尨蠹伊私釩SS對(duì)背景圖片控制的技巧2009-11-11
發(fā)一個(gè)比較漂亮的選項(xiàng)卡動(dòng)態(tài)增刪的效果
發(fā)一個(gè)比較漂亮的選項(xiàng)卡動(dòng)態(tài)增刪的效果...2007-09-09
javascript 靜態(tài)樹菜單實(shí)現(xiàn)代碼
coding這么多年,我居然不知道有"dl"這種html標(biāo)記,慚愧啊,今天偶然發(fā)現(xiàn),覺得好玩,如是我為它添加了折疊功能,變成了一顆靜態(tài)樹.2010-12-12
用js實(shí)現(xiàn)網(wǎng)頁上模仿桌面右鍵菜單
用js實(shí)現(xiàn)網(wǎng)頁上模仿桌面右鍵菜單...2007-03-03

