刚到了一个公司面试,这是他们面试给出的最后以个题目....
用JavaScript实现一个连动菜单.
下面是我写出来的,也不知道对不对,面试肯定是过不了了....555555.....
<html>
<head>
<title></title>
<script type="text/javascript">
function change(){
var leftShow = new Array();
leftShow[0] = "中国";
leftShow[1] = "美国";
var rightShow = new Array();
rightShow[0] = new Array();
rightShow[0][0] = "北京";
rightShow[0][1] = "上海";
rightShow[0][2] = "重庆";
rightShow[1] = new Array();
rightShow[1][0] = "休斯顿";
rightShow[1][1] = "西雅图";
rightShow[1][2] = "纽约";
var s = document.getElementById("selectLeft").value;
var selectRight = document.getElementById("selectRight");
//alert(s);
if(s == "0"){
for(var j=0;j<rightShow[0].length;j++){
selectRight.remove(i);
}
for(var i=0;i<rightShow[0].length;i++){
var detailShow = new Option(rightShow[0][i]);
//document.getElementById("selectRight").insertBefore(detailShow,selectRight.options[i]);
selectRight.appendChild(detailShow);
}
}
else if(s == "1"){
for(var j=0;j<rightShow[1].length;j++){
selectRight.remove(i);
}
for(var i=0;i<rightShow[1].length;i++){
var detailShow = new Option(rightShow[1][i]);
///document.getElementById("selectRight").insertBefore(detailShow,);
obj = document.getElementById("selectRight").appendChild(detailShow);
}
}
}
</script>
</head>
<body>
<select id="selectLeft" οnchange="change()">
<option value="0" selected="selected">中国</option>
<option value="1">美国</option>
</select>
<select id="selectRight">
<option>北京</option>
<option>上海</option>
<option>重庆</option>
</select>
</body>
</html>