teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

コードの追記

2019/09/16 02:42

投稿

nanase21
nanase21

スコア144

title CHANGED
File without changes
body CHANGED
@@ -34,11 +34,8 @@
34
34
  ```
35
35
 
36
36
  #追記
37
- kei344さんからご教示いただいコードをjs対応させました。
37
+ kei344さんとhatena19さんからご教示いただいコードを参考以下のように修正する事で解決しました。
38
- 下記のコードでバックグランドカラー変わる``class``を削除しようしたところ動かなくなりました。
38
+ ご教示頂きありがとうございました。
39
- ```js
40
- document.getElementsByClassName("div-btn").classList.remove("active");
41
- ```
42
39
 
43
40
  ```css
44
41
  .active {
@@ -48,9 +45,13 @@
48
45
 
49
46
  ```js
50
47
  function func_1(button){
48
+ var elements = document.getElementsByClassName("div-btn");
49
+ for(i=0;i<elements.length;i++){
51
- document.getElementsByClassName("div-btn").classList.remove("active");
50
+ elements[i].classList.remove("active");
51
+ }
52
52
  button.classList.add("active");
53
53
  let input = button.firstElementChild.textContent;
54
54
  document.getElementById("res").value = input;
55
55
  }
56
+ }
56
57
  ```

1

試したことを追記

2019/09/16 02:42

投稿

nanase21
nanase21

スコア144

title CHANGED
File without changes
body CHANGED
@@ -31,4 +31,26 @@
31
31
  let input = document.getElementById("input1").textContent;
32
32
  document.getElementById("res").value = input;
33
33
  }
34
+ ```
35
+
36
+ #追記
37
+ kei344さんからご教示いただいたコードをjsに対応させました。
38
+ 下記のコードでバックグランドカラーが変わる``class``を削除しようとしたところ動かなくなりました。
39
+ ```js
40
+ document.getElementsByClassName("div-btn").classList.remove("active");
41
+ ```
42
+
43
+ ```css
44
+ .active {
45
+ background-color: red;
46
+ }
47
+ ```
48
+
49
+ ```js
50
+ function func_1(button){
51
+ document.getElementsByClassName("div-btn").classList.remove("active");
52
+ button.classList.add("active");
53
+ let input = button.firstElementChild.textContent;
54
+ document.getElementById("res").value = input;
55
+ }
34
56
  ```