Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> menu2level.html </title>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
<Script type = "text/javascript">
Function loadXML (){
Var xmlDoc;
Try {
// IE
XmlDoc = new ActiveXObject ("Microsoft. XMLDOM ");
} Catch (e ){
Try {
XmlDoc = document. implementation. createDocument ("", "", null );
} Catch (e ){
Alert (e. message );
Return;
}
}
XmlDoc. async = false;
XmlDoc. load ("cities. xml ");
Return xmlDoc;
}
// After a webpage is loaded, it is loaded in the province where the webpage is loaded.
Onload = function (){
Var xmlDocument = loadXML ();
Var provinceArr = xmlDocument. getElementsByTagName ("province ");
Var proSize = provinceArr. length;
For (var I = 0; I <proSize; I ++ ){
// Create an option Node
Var optionElement = document. createElement ("option ");
Var provinceName = provinceArr [I]. getAttribute ("name ");
// Create a text node
Var textElement = document. createTextNode (provinceName );
OptionElement. appendChild (textElement );
OptionElement. setAttribute ("value", provinceName );
Var node = document. getElementById ("province ");
Node. appendChild (optionElement );
}
}
// Province change event
Function changeProvince (node ){
// Obtain the selected badge
Var index = node. selectedIndex;
// Obtain the corresponding province name
Var provinceName = node. options [index]. value;
LoadCities (provinceName );
}
// Load the city information according to the province ID
Function loadCities (proName ){
Var xmlDocument = loadXML ();
Var provinceArr = xmlDocument. getElementsByTagName ("province ");
// Obtain the elements of a city
Var citySelectEle = document. getElementById ("cities ");
Var size = citySelectEle. options. length;
For (var I = size; I> 0; I --){
CitySelectEle. remove (I );
}
// Obtain the number of provinces
Var proSize = provinceArr. length;
Var proElement;
// Obtain the corresponding province Element
For (var I = 0; I <proSize; I ++ ){
If (provinceArr [I]. getAttribute ("name") = proName ){
ProElement = provinceArr [I];
Break;
}
}
// Obtain the city information of a province
Var citiesArr = proElement. getElementsByTagName ("city ");
Var len = citiesArr. length;
For (var I = 0; I <len; I ++ ){
// Create an option Node
Var optionElement = document. createElement ("option ");
// Obtain the city name
Var cityName = citiesArr [I]. firstChild. nodeValue;
// Create a text node
Var textElement = document. createTextNode (cityName );
OptionElement. appendChild (textElement );
OptionElement. setAttribute ("value", cityName );
CitySelectEle. appendChild (optionElement );
}
}
Function getValue (){
Var pro = document. getElementById ("province"). value;
Var city = document. getElementById ("cities"). value;
Alert (pro + ":" + city );
}
</Script>
</Head>
<Body>
<Select id = "province" onchange = "changeProvince (this)">
<Option value = "" selected = "selected"> -- Province -- </option>
</Select>
<Select id = "cities">
<Option value = "" selected = "selected"> -- City -- </option>
</Select>
<Input type = "button" value = "pop-up" onclick = "getValue ()"/>
</Body>
</Html>
The effect is as follows:
Http://img.blog.csdn.net/20140315235043343? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ==/ dissolve/70/gravity/SouthEast
The cities. xml file is as follows:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Xml-body>
<Province name = "Shaanxi">
<City> Xi'an </city>
<City> Hanzhong </city>
<City> Baoji </city>
<City> yan'an </city>
</Province>
<Province name = "Guangdong">
<City> Foshan </city>
<City> Shenzhen </city>
<City> Guangzhou </city>
<City> Shantou </city>
</Province>
<Province name = "Liaoning">
<City> Dalian </city>
<City> Tieling </city>
<City> Anshan </city>
<City> Fushun </city>
</Province>
</Xml-body>