回答編集履歴

1

chosei

2021/03/20 07:52

投稿

yambejp
yambejp

スコア116690

test CHANGED
@@ -57,3 +57,135 @@
57
57
  </label>
58
58
 
59
59
  ```
60
+
61
+ # 調整版
62
+
63
+ ```jaascript
64
+
65
+ <script>
66
+
67
+ window.addEventListener('DOMContentLoaded', ()=>{
68
+
69
+ const s1=document.querySelector('#s1');
70
+
71
+ const s2=s1.cloneNode(true);
72
+
73
+ const p=s1.parentNode;
74
+
75
+ s2.setAttribute('id','s2');
76
+
77
+ s2.querySelectorAll('.hide').forEach(x=>x.remove());
78
+
79
+ document.querySelector('#color').addEventListener('change',()=>{
80
+
81
+ if([...p.children].includes(s1)){
82
+
83
+ p.insertBefore(s2,s1);
84
+
85
+ s1.remove();
86
+
87
+ }else{
88
+
89
+ p.insertBefore(s1,s2);
90
+
91
+ s2.remove();
92
+
93
+ }
94
+
95
+ });
96
+
97
+ document.addEventListener('change',()=>{
98
+
99
+ const color=document.querySelector('#s1,#s2').value;
100
+
101
+ document.querySelectorAll('.s1_select').forEach(x=>{
102
+
103
+ x.style.display='none';
104
+
105
+ x.querySelector('select').disabled=true;
106
+
107
+ });
108
+
109
+ document.querySelector('#'+color).style.display='';
110
+
111
+ document.querySelector('#'+color+" select").disabled=false;
112
+
113
+ ;
114
+
115
+ });
116
+
117
+ });
118
+
119
+ </script>
120
+
121
+ <label><input type="checkbox" id="color" name="color"> カラー</label>
122
+
123
+ <label>
124
+
125
+ <select id="s1">
126
+
127
+ <option value="white" class="hide">白</option>
128
+
129
+ <option value="red">赤</option>
130
+
131
+ <option value="black" class="hide">黒</option>
132
+
133
+ </select>
134
+
135
+ </label>
136
+
137
+
138
+
139
+ <div class="s1_select" id="white">
140
+
141
+ <label>
142
+
143
+ <select>
144
+
145
+ <option value="white1">WHITE1</option>
146
+
147
+ <option value="white2">WHITE2</option>
148
+
149
+ </select>
150
+
151
+ </label>
152
+
153
+ </div>
154
+
155
+
156
+
157
+ <div class="s1_select" id="red" style="display:none">
158
+
159
+ <label>
160
+
161
+ <select disabled>
162
+
163
+ <option value="red1">RED1</option>
164
+
165
+ <option value="red2">RED2</option>
166
+
167
+ </select>
168
+
169
+ </label>
170
+
171
+ </div>
172
+
173
+
174
+
175
+ <div class="s1_select" id="black" style="display:none">
176
+
177
+ <label>
178
+
179
+ <select disabled>
180
+
181
+ <option value="black1">BLACK1</option>
182
+
183
+ <option value="black2">BLACK2</option>
184
+
185
+ </select>
186
+
187
+ </label>
188
+
189
+ </div>
190
+
191
+ ```