关于7-8的练习题
来源:7-8 编程练习
qq_aiq款_hnrGLT
2019-08-16 16:11:24
<!DOCTYPE html> <html> <head> <title>第四天学习-CSS</title> <link rel="stylesheet" type="text/css" href="../css/day04.css"> <script type="text/javascript" src="../js/day04.js"> </script> </head> <body onload="show(),address()"> <label>省份:</label> <select name="sheng" id="sheng" onclick="linkage()"></select> <label>市区:</label> <select name="shi" id="shi"></select> </body> </html>
function address(){
/*请在此补充代码*/
var sheng = document.getElementById("sheng");
var shi = document.getElementById("shi");
selectAdd(sheng,"北京省",1);
selectAdd(sheng,"天津省",2);
selectAdd(sheng,"河北省",3);
selectAdd(sheng,"河南省",4);
}
function selectAdd(obj,name,value){
obj.options.add(new Option(name,value));
}
function linkage(){
/*请在此补充代码*/
var sheng = document.getElementById("sheng").value;
var shi = document.getElementById("shi");
if(sheng==1){
document.getElementById("shi").length=0;
selectAdd(shi,"朝阳区",5);
selectAdd(shi,"西城区",6);
}
if(sheng==2){
document.getElementById("shi").length=0;
selectAdd(shi,"西青区",7);
selectAdd(shi,"武清区",8);
}
if(sheng==3){
document.getElementById("shi").length=0;
selectAdd(shi,"石家庄",9);
selectAdd(shi,"世家区",10);
}
if(sheng==4){
document.getElementById("shi").length=0;
selectAdd(shi,"郑州市",11);
selectAdd(shi,"开封市",12);
}
}请老师帮忙批阅下,我对照了其他同学的代码,感觉我和其他同学的写法不一样的!
1回答
同学你好!
同学这样也是可以的,但是有一些需要优化的地方
同学的js中有show()这个函数吗?如果没有的话将html中的show()函数去掉,否则会报错

由于默认的省份是北京,建议同学将城市的默认初始化为背景下面的城市

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~