关于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回答

好帮手慕柯南

2019-08-16

同学你好!

同学这样也是可以的,但是有一些需要优化的地方

  1. 同学的js中有show()这个函数吗?如果没有的话将html中的show()函数去掉,否则会报错

    http://img.mukewang.com/climg/5d5674b40001e8f603670092.jpg

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

    http://img.mukewang.com/climg/5d5675140001e4bc05780277.jpg

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

0
hq_aiq款_hnrGLT
h 谢谢老师,show()这个方法是我忘记删掉了!
h019-08-19
共1条回复

0 学习 · 9666 问题

查看课程

相似问题

7-6 编程练习

回答 1

作业 7-8

回答 1

7-8编程练习

回答 1

7-8编程练习

回答 1

7-8编程练习

回答 2