-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtab切换栏.html
91 lines (84 loc) · 2.82 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>tab切换栏</title>
<style>
.head{
height: 40px;
font-size: 0;
}
.title{
height: 40px;
width: 100px;
background-color: rgb(185, 185, 185);
display: inline-block;
margin: 0;
font-size: 12px;
text-align: center;
line-height: 40px;
cursor: pointer;
}
.change{
background-color: red;
}
.text{
display: none;
}
.text:nth-child(1){
display: block;
}
</style>
</head>
<body>
<div class="head">
<div class="title change">1</div>
<div class="title">2</div>
<div class="title">3</div>
<div class="title">4</div>
</div>
<div class="content">
<div class="text ">aaa</div>
<div class="text ">bbb</div>
<div class="text ">ccc</div>
<div class="text ">ccc</div>
</div>
</body>
<script>
// window.onload=function(){
// let title = document.getElementsByClassName('title');
// let text = document.getElementsByClassName('text');
// for(let j = 0;j<title.length;j++){
// title[j].onclick = function(){
// for(let i = 0;i<title.length;i++){
// //排他思想 干掉所有人
// title[i].className = 'title';
// text[i].style.display='none';
// // console.log(i);
// }
// // console.log(i); //i=5
// this.className = 'title change';
// console.log('j='+j);
// text[j].style.display='block';
// }
// }
// };
/********自定义属性法********/
let title = document.getElementsByClassName('title');
let text = document.querySelectorAll('.text');
for(let j = 0;j<title.length;j++){
title[j].setAttribute('index',j);
title[j].onclick = function(){
for(let i = 0;i<title.length;i++){
title[i].className = 'title';
text[i].style.display='none';
}
this.className = 'title change';
var index = this.getAttribute('index');
text[index].style.display = 'block';
}
}
</script>
</html>