HTML的dataset属性
2021.05.14 Fri
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
<style>
* {
margin: 0;
padding: 0;
}
.content {
padding: 200px;
text-align: center;
width: 800px;
height: 100vh;
margin: auto;
}
button {
width: 80px;
height: 40px;
margin: 10px;
outline: none;
border: none;
cursor: pointer
border-radius: 5px;
}
</style>
<body>
<div class="content">
<button>click</button>
<button>click</button>
<button>click</button>
<button>click</button>
<button>click</button>
</div>
<script>
const button = document.querySelectorAll('button');
const color = ['lightblue', 'lightcoral', 'lightcyan', 'lightpink', 'lightgreen'];
color.map((item, index) => {
button[index].dataset.color = item;
button[index].style.background = item;
button[index].textContent = item;
});
button.forEach((item, index) => {
item.addEventListener('click', () => {
const btnColor = item.dataset.color;
document.querySelector('body').style.background = btnColor;
});
});
</script>
</body>

检测到页面内容有更新,是否刷新页面