首先建立一个area.html,很简单包含一个省份的select元素和一个城市的界面元素:
<html>
<head>
<title>area demo</title>
</head>
<body>
<select id=”province” size=1 onchange=”loadcity()”>
<option value=”city01.xml”> 江苏 </option>
<option value=”city02.xml”> º湖南 </option>
<option value=”city03.xml”> 湖北 </option>
</select>
<select id=”city” size=1>
</select>
<xml id=”xmlobj”></xml>
<script language=”javascript” type=”text/javascript”>
var provobj = document.all(“province”);
var cityobj = document.all(“city”);
var xmlhttp = document.all(“xmlobj”);
loadcity();
/**
* 装载城市数据
*/
function loadcity() {
cityobj.options.length = 0;
var file = provobj.options[provobj.selectedindex].value;
xmlhttp.async = false;
xmlhttp.load(file);
var cities = xmlhttp.selectnodes(“cities/city”);
var idx,name;
for(idx = 0; idx < cities.length; idx ++) {
name = cities[idx].getattribute(“name”);
cityobj.options.length++;
cityobj.options[cityobj.options.length – 1].value = name;
cityobj.options[cityobj.options.length – 1].text = name;
}
}
</script>
</body>
</html>
然后建立三个省份的城市数据文件,分别命名为city01.xml,city02.xml,city03.xml
<?xml version=”1.0″ encoding=”gb2312″?>
<cities province=”江苏”>
<city name=”南京”/>
</cities>
<?xml version=”1.0″ encoding=”gb2312″?>
<cities province=”湖南”>
<city name=”长沙”/>
</cities>
<?xml version=”1.0″ encoding=”gb2312″?>
<cities province=”湖北”>
<city name=”武汉”/>
</cities>
保存,在ie6浏览通过,理论上可以在ie5一上浏览器跑。