回答編集履歴
1
コード追加
test
CHANGED
@@ -3,3 +3,71 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
[CSS変数をJavaScriptで動的に変更し、複数のスタイルに一括適用する \| Black Everyday Company](https://kuroeveryday.blogspot.com/2018/03/change-css-valiables-by-javascript.html)
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
```html
|
10
|
+
|
11
|
+
<input type="button" value="変更" onclick="change()">
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
<input type="button" value="test">
|
16
|
+
|
17
|
+
<input type="button" value="test" class="btn_color">
|
18
|
+
|
19
|
+
<input type="button" value="test">
|
20
|
+
|
21
|
+
<input type="button" value="test" class="btn_color">
|
22
|
+
|
23
|
+
<input type="button" value="test">
|
24
|
+
|
25
|
+
<input type="button" value="test" class="btn_color">
|
26
|
+
|
27
|
+
```
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
```css
|
32
|
+
|
33
|
+
:root {
|
34
|
+
|
35
|
+
--btn-color1: green;
|
36
|
+
|
37
|
+
--btn-color2: orange;
|
38
|
+
|
39
|
+
}
|
40
|
+
|
41
|
+
input[type="button"].btn_color{
|
42
|
+
|
43
|
+
color: #000;
|
44
|
+
|
45
|
+
background-color: var(--btn-color1);
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
input[type="button"] {
|
50
|
+
|
51
|
+
color: #000;
|
52
|
+
|
53
|
+
background-color: var(--btn-color2);
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
```js
|
62
|
+
|
63
|
+
function change() {
|
64
|
+
|
65
|
+
const root = document.documentElement;
|
66
|
+
|
67
|
+
root.style.setProperty("--btn-color1","red");
|
68
|
+
|
69
|
+
root.style.setProperty("--btn-color2","blue");
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
```
|