質問編集履歴

5

タイトルを変更

2018/05/11 04:27

投稿

hibikikudo
hibikikudo

スコア238

test CHANGED
@@ -1 +1 @@
1
- 冗長なif文化したい
1
+ 1つ目のテーブルのtdクリックすると、2つ目のテーブルの同じ位置にあるtd縦一の背景色を変更させたい
test CHANGED
File without changes

4

テーブル二つの場合に絞る

2018/05/11 04:27

投稿

hibikikudo
hibikikudo

スコア238

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- 最上段ルをクリックすると縦列の背景色わるテーブルを作りたい。
3
+ 1つ目テーブのtdをクリックすると、2つ目のテーブルの同じ位置にあるtd列の背景色更させたい。
4
4
 
5
5
 
6
6
 
@@ -17,6 +17,30 @@
17
17
  ```html
18
18
 
19
19
  <table id="table">
20
+
21
+ <tbody>
22
+
23
+ <tr>
24
+
25
+ <td>1</td>
26
+
27
+ <td>2</td>
28
+
29
+ <td>3</td>
30
+
31
+ <td>4</td>
32
+
33
+ <td>5</td>
34
+
35
+ </tr>
36
+
37
+ </tbody>
38
+
39
+ </table>
40
+
41
+
42
+
43
+ <table id="targetTable">
20
44
 
21
45
  <tbody>
22
46
 
@@ -118,31 +142,33 @@
118
142
 
119
143
  var selected_td = $("#table td");
120
144
 
145
+ var target_td = $("#targetTable td");
146
+
121
147
  $(selected_td).on("click", function() {
122
148
 
123
- var nthItem = selected_td.index(this);
149
+ var nthItem = $("#table td").index(this);
124
150
 
125
- selected_td.removeClass("is-selected");
151
+ target_td.removeClass("is-selected");
126
152
 
127
153
  if (nthItem === 0) {
128
154
 
129
- $("#table td:nth-child(1)").addClass("is-selected");
155
+ $("#targetTable td:nth-child(1)").addClass("is-selected");
130
156
 
131
157
  } else if (nthItem === 1) {
132
158
 
133
- $("#table td:nth-child(2)").addClass("is-selected");
159
+ $("#targetTable td:nth-child(2)").addClass("is-selected");
134
160
 
135
161
  } else if (nthItem === 2) {
136
162
 
137
- $("#table td:nth-child(3)").addClass("is-selected");
163
+ $("#targetTable td:nth-child(3)").addClass("is-selected");
138
164
 
139
165
  } else if (nthItem === 3) {
140
166
 
141
- $("#table td:nth-child(4)").addClass("is-selected");
167
+ $("#targetTable td:nth-child(4)").addClass("is-selected");
142
168
 
143
169
  } else if (nthItem === 4) {
144
170
 
145
- $("#table td:nth-child(5)").addClass("is-selected");
171
+ $("#targetTable td:nth-child(5)").addClass("is-selected");
146
172
 
147
173
  }
148
174
 
@@ -158,8 +184,6 @@
158
184
 
159
185
  ### サンプル
160
186
 
161
- [テーブル一つ](https://jsfiddle.net/190zk4k3/2/)
162
-
163
187
  [テーブル二つ](https://jsfiddle.net/190zk4k3/3/)
164
188
 
165
189
  どうぞよろしくお願いいたします。

3

テーブル二つの場合を追加

2018/05/11 03:55

投稿

hibikikudo
hibikikudo

スコア238

test CHANGED
File without changes
test CHANGED
@@ -158,8 +158,8 @@
158
158
 
159
159
  ### サンプル
160
160
 
161
- [JSFiddle](https://jsfiddle.net/190zk4k3/1/)
161
+ [テーブル一つ](https://jsfiddle.net/190zk4k3/2/)
162
162
 
163
-
163
+ [テーブル二つ](https://jsfiddle.net/190zk4k3/3/)
164
164
 
165
165
  どうぞよろしくお願いいたします。

2

タイトルを変更

2018/05/11 03:27

投稿

hibikikudo
hibikikudo

スコア238

test CHANGED
@@ -1 +1 @@
1
- 冗長なif else文を配列化したい
1
+ 冗長なif文を配列化したい
test CHANGED
File without changes

1

コード修正

2018/05/11 03:06

投稿

hibikikudo
hibikikudo

スコア238

test CHANGED
File without changes
test CHANGED
@@ -120,7 +120,7 @@
120
120
 
121
121
  $(selected_td).on("click", function() {
122
122
 
123
- var nthItem = $("#table td").index(this);
123
+ var nthItem = selected_td.index(this);
124
124
 
125
125
  selected_td.removeClass("is-selected");
126
126