所用的XML文档结构(area.xml文件名)
<?xml version="1.0" encoding="utf-8" ?>
<country>
<pro id="jsu" name="江苏">
<city id="wuxi" name="无锡">
<area id="xinqu" name="新区"></area>
</city>
<city id="nanjing" name="南京"></city>
</pro>
<pro id="anhui" name="安徽">
<city id="hefei" name="合肥">
<area id="xinqu" name="新区"></area>
</city>
</pro>
</country>页面的代码
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>联动</title>
<style type="text/css">
*{ padding:0;margin:0; }
body{width:1200px;height:700px;}
</style>
<script type="text/javascript">
function xmlHttp(){
if(window.XMLHttpRequest){
return new XMLHttpRequest();
}else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}else {
return new ActiveXObject("Msxml2.XMLHTTP");
}
}function loadXML(){
var xmlDoc = "";
var xmlhttp = xmlHttp();
xmlhttp.open("GET",'area.xml',false);
xmlhttp.send(null);
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
xmlDoc = xmlhttp.responseXML.documentElement;
}
}
return xmlDoc
}window.onload = function(){
var doc = loadXML();
var pro = document.querySelector("#pro");
var city = document.querySelector("#city");
var area = document.querySelector("#area");
//获取所有省份
var nodeList=doc.getElementsByTagName("pro");for(var i = 0; i < nodeList.length ; i++){
var node = nodeList[i];
var option = document.createElement('option');
var txt = document.createTextNode(node.getAttribute("name"));
option.appendChild(txt);
//option.nodeValue = node.getAttribute("name");
option.id = node.id;
//getAttribute返回元素节点的指定属性值
//getAttributeNode返回指定的属性节点
pro.appendChild(option);
}
var nodeListCity = null;
//给省添加change事件
pro.addEventListener('change',function(e){
if(city.options.length >= 2){
clear(city);
}
var index = this.selectedIndex;
var id = this.options[index].id;
//this.options[index].value;
for(var i =0 ; i < nodeList.length ; i++){
if(id == nodeList[i].id){
nodeListCity = nodeList[i].querySelectorAll("city");
break;
}
}
if(nodeListCity){
for(var i = 0 ; i < nodeListCity.length;i++){
var cy = nodeListCity[i];
var option = document.createElement("option");
var txt = document.createTextNode(cy.getAttribute("name"));
option.appendChild(txt);
option.id = cy.id;
city.appendChild(option);
}
}else{
//无数据的时候
var option = document.createElement("option");
var txt = document.createTextNode("no data");
option.appendChild(txt);
city.appendChild(option);
}
},false);var nodeListArea = null;
//给城市添加change事件
city.addEventListener('change',function(){
if(area.options.length >= 2){
clear(area);
}
var index = this.selectedIndex;
var id = this.options[index].id;for(var i =0 ; i < nodeListCity.length ; i++){
if(id == nodeListCity[i].id){
nodeListArea = nodeListCity[i].querySelectorAll("area");
break;
}
}if(nodeListArea){
for(var i = 0 ; i < nodeListArea.length;i++){
var cy = nodeListArea[i];
var option = document.createElement("option");
var txt = document.createTextNode(cy.getAttribute("name"));
option.appendChild(txt);
option.id = cy.id;
area.appendChild(option);
}
}else{
//无数据的时候
var option = document.createElement("option");
var txt = document.createTextNode("no data");
option.appendChild(txt);
area.appendChild(option);
}},false);
};
//删除关联的子select的option
function clear(obj){
if(obj.nodeName.toLowerCase() === "select"){
var len = obj.options.length;
if(len == 1){
obj.options[0].selected = true;
return;
}
for(; 2 <= obj.options.length ;){
obj.remove(1);
}
}
}
</script>
</head>
<body>
<form>
<select id='pro'>
<option>省/市直辖市</option>
</select>
<select id='city'>
<option>请选择</option>
</select>
<select id='area'>
<option>请选择</option>
</select>
</form>
</body>
</html>
转载于:https://my.oschina.net/u/1451372/blog/529447