回答編集履歴

2

fix bug

2022/10/02 05:21

投稿

Cocode
Cocode

スコア2314

test CHANGED
@@ -23,19 +23,19 @@
23
23
  SELECT_BOXES.forEach(select => select.addEventListener('change', changeColor));
24
24
 
25
25
  function changeColor(e) {
26
- if (e.target.value) {
26
+ if (e.target.value == 0) {
27
+ e.target.style.backgroundColor = 'white';
28
+ } else {
27
29
  e.target.style.backgroundColor = 'orange';
28
- } else {
29
- e.target.style.backgroundColor = 'white';
30
30
  }
31
31
  }
32
32
  ```
33
- - **動作確認サンプル:** https://jsfiddle.net/mpwuhsxb/
33
+ - **動作確認サンプル:** https://jsfiddle.net/j74dazm5/
34
34
 
35
35
  ### 解説
36
36
  `要素.addEventListener(種類, 関数名)`で、1つの要素に1つのイベントをつけることができます。
37
37
  - 種類には`'change'`, `'click'`, `'mouseover'`, `'keydown'`など色々あります。
38
- - 関数名のところには、❌`myFunct()`とは書かず → ✅`myFunc`と( )なしで書きましょう。
38
+ - 関数名のところには、❌`myFunc()`とは書かず → ✅`myFunc`と( )なしで書きましょう。
39
39
 
40
40
  ---
41
41
 

1

if and sample

2022/10/02 05:14

投稿

Cocode
Cocode

スコア2314

test CHANGED
@@ -23,13 +23,14 @@
23
23
  SELECT_BOXES.forEach(select => select.addEventListener('change', changeColor));
24
24
 
25
25
  function changeColor(e) {
26
- if (e.target.value == 0) {
26
+ if (e.target.value) {
27
+ e.target.style.backgroundColor = 'orange';
28
+ } else {
27
29
  e.target.style.backgroundColor = 'white';
28
- } else {
29
- e.target.style.backgroundColor = 'orange';
30
30
  }
31
31
  }
32
32
  ```
33
+ - **動作確認サンプル:** https://jsfiddle.net/mpwuhsxb/
33
34
 
34
35
  ### 解説
35
36
  `要素.addEventListener(種類, 関数名)`で、1つの要素に1つのイベントをつけることができます。