-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtab.html
103 lines (92 loc) · 2.17 KB
/
tab.html
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
92
93
94
95
96
97
98
99
100
101
102
103
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
font-size: 13px;
list-style: none;
}
.menu {
width: 210px;
margin: 50px auto;
border: 1px solid #ccc;
}
.menu p {
height: 25px;
line-height: 25px;
font-weight: bold;
background: #eee;
border-bottom: 1px solid #ccc;
cursor: pointer;
padding-left: 5px;
}
.menu div ul {
display: none;
}
.menu li {
height: 24px;
line-height: 24px;
padding-left: 5px;
}
</style>
<script type="text/javascript">
window.onload = function () {
// 将所有点击的标题和要显示隐藏的列表取出来
var titles = document.getElementsByTagName("p");
var divs = document.getElementsByTagName("ul")
console.log(divs)
console.log(titles)
// 遍历所有要点击的标题且给它们添加索引及绑定事件
for (var i = 0; i < titles.length; i++) {
titles[i].id = i;
titles[i].onclick = function () {
//alert(this.id)
// 获取点击的标题上的索引属性,根据该索引找到对应的列表
var p = divs[this.id]
// 判断该列表,如果是显示的则将其隐藏,如果是隐藏的则将其显示出来
if (p.style.display == "block") {
p.style.display = "none"
} else {
for (var j = 0; j < divs.length; j++) {
divs[j].style.display = "none"
}
p.style.display = "block"
}
}
}
}
</script>
</head>
<body>
<div class="menu" id="menu">
<div>
<p>Web前端</p>
<ul style="display:block">
<li>JavaScript</li>
<li>DIV+CSS</li>
<li>jQuery</li>
</ul>
</div>
<div>
<p>后台脚本</p>
<ul>
<li>PHP</li>
<li>ASP.net</li>
<li>JSP</li>
</ul>
</div>
<div>
<p>前端框架</p>
<ul>
<li>Extjs</li>
<li>Esspress</li>
<li>YUI</li>
</ul>
</div>
</div>
</body>
</html>