<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd> <Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/> <Title> dynamically create a vertical list </title> <Style type = "text/css"> A {color: #000; text-decoration: none ;} A: hover {color: # F00 ;} # Menu {width: 100px; border: 1px solid # CCC; border-bottom: none ;} # Menu ul {list-style: none; margin: 0px; padding: 0px ;} # Menu ul li {background: # eee; padding: 0px 8px; height: 26px; line-height: 26px; border-bottom: 1px solid # CCC; position: relative ;} # Menu ul li ul {display: none; position: absolute; left: 100px; top: 0px; width: 100px; border: 1px solid # ccc; border-bottom: none ;} # Menu ul li. current ul {display: block ;} # Menu ul li {text-align: center;}/* set City content center */ </Style> <Script type = "text/javascript"> Var provs = {"Jiangxi Province": ["Nanchang city", "Jingdezhen", "Jiujiang", "Yingtan", "Pingxiang", "Xinyu", "Ganzhou ", "Ji 'an", "Yichun", "Fuzhou", "Shangrao"], "Fujian Province": ["Fuzhou", "Xiamen", "Putian", "Sanming", "Quanzhou", "Zhangzhou", "Nanping", "Longyan ", "Ningde"], "Hebei Province": ["Shijiazhuang", "Handan", "Xingtai", "Baoding", "Zhangjiakou", "Chengde", "Langfang", "Tangshan ", "Qinhuangdao", "Cangzhou", "Hengshui"], "Sichuan Province": ["Chengdu", "Zigong City", "Panzhihua City", "Luzhou city", "Deyang city", "Mianyang City", "Guangyuan City", "Suining city ", "Neijiang City", "Leshan City", "Nanchong City", "Meishan City", "Yibin City", "Guang 'an city", "dazhou City", "ya'an City", "Bazhong City", "Ziyang City ", "ABA Tibetan and Qiang Autonomous Prefecture", "Ganzi Tibetan Autonomous Prefecture", "Liangshan Yi Autonomous Prefecture"], "Shanxi Province": ["Taiyuan City", "Datong City", "Yangquan City", "Changzhi City", "Jincheng City", "Shuozhou City", "Jinzhong City", "Yuncheng City ", "Xinzhou City", "Linfen city", "Luliang City"], "Inner Mongolia": ["Hohhot", "Baotou City", "Wuhai City", "Chifeng City", "Tongliao City", "Erdos city", "Hulunbeier City", "bayannaoer City ", "ulanchabu City", "Xing 'an League", "xilingol League", "Alxa League"], "Hainan Province": ["Haikou City", "Sanya City"], "Chongqing": ["Chongqing"], "Guizhou Province": ["Guiyang City", "liupanshui City", "Zunyi City", "Anshun City", "Tongren region", "Buyi and Miao Autonomous Prefecture in Southwest Guizhou", "Bijie region ", "Qiandongnan Miao and Dong Autonomous Prefecture", "Qiandongnan Buyi and Miao Autonomous Prefecture"], "Gansu Province": ["Lanzhou City", "Jiayuguan City", "Jinchang city", "Baiyin City", "Tianshui city", "Wuwei City", "Changzhi City", "Pingliang city ", "Jiuquan City", "Qingyang City", "Dingxi city", "Longnan City", "Linxia Hui Autonomous Prefecture", "Gannan Tibetan Autonomous Prefecture"], "Qinghai Province": ["Xining city", "Haidong region", "Haibei Tibetan Autonomous Prefecture", "Huangnan Tibetan Autonomous Prefecture", "Hainan Tibetan Autonomous Prefecture", "Guoluo Tibetan Autonomous Prefecture", "Yushu Tibetan Autonomous Prefecture ", "haixi Mongolian Tibetan Autonomous Prefecture"], "NingXia Autonomous Region": ["Yinchuan City", "Shizuishan city", "wuzhong City", "fixed district", "Zhongwei City"] }; Function iniEvent (){ Var provUL = document. getElementById ("prov "); If (provUL ){ Var allli = provUL. getElementsByTagName ("li "); For (I = 0; I <allli. length; I ++ ){ Node = allli [I]; Node. onmouseover = function () {// display layer when the mouse passes This. className = "current "; } Node. onmouseout = function () {// hide layer when the mouse leaves This. className = this. className. replace ("current ",""); } } } } Function loadData (){ Var provUL = document. getElementById ("prov "); Var nIndex = 0; For (var key in provs ){ Var provLi = document. createElement ("li "); ProvLi. id = "provLI" + nIndex; ProvLi. innerHTML = "<a href = '#'>" + key + "</a> "; ProvUL. appendChild (provLi); // Add the province li // ============================ Add a city ==== Var citys = provs [key]; If (citys. length> 0 ){ Var cityUL = document. createElement ("ul "); Var maxLength = 0; // The length of the content in the largest city, so that the maximum width of cityUL can be set later to achieve adaptive width. For (var I = 0; I <citys. length; I ++ ){ Var cityName = citys [I]; If (cityName. length> maxLength ){ MaxLength = cityName. length; // extract the city with the maximum length } Var cityLI = document. createElement ("li "); CityLI. id = "cityLI" + I; CityLI. innerHTML = "<a href = '#'>" + cityName + "</a> "; CityUL. appendChild (cityLI ); } If (maxLength <= 6 ){ MaxLength = 100; } Else { MaxLength = maxLength * 20; // Multiply the value by 20 to calculate the value by 20 PX. } MaxLength = maxLength + "px"; // Add the pixel suffix CityUL. style. width = maxLength; // you can specify the maximum width of cityUL. ProvLi. appendChild (cityUL); // Add a city UL } NIndex ++; } IniEvent (); // initialization event } </Script> </Head> <Body onload = "loadData ()"> <Div id = "menu"> <Ul id = "prov"> </Ul> </Div> </Body> </Html> |