How to use javascript to dynamically create provincial/municipal portrait list menus using dom

Source: Internet
Author: User

How to use javascript to dynamically create provincial/municipal portrait list menus using dom

This example describes how to use javascript to dynamically create provincial/municipal vertical list menus by using dom. Share it with you for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

<! 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>

I hope this article will help you design javascript programs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.