質問編集履歴
5
タイトルを変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
1つ目のテーブルのtdをクリックすると、2つ目のテーブルの同じ位置にあるtd縦一列の背景色を変更させたい
|
body
CHANGED
File without changes
|
4
テーブル二つの場合に絞る
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
|
-
|
2
|
+
1つ目のテーブルのtdをクリックすると、2つ目のテーブルの同じ位置にあるtd縦一列の背景色を変更させたい。
|
3
3
|
|
4
4
|
### 発生している問題・エラーメッセージ
|
5
5
|
if文で書いたコードで問題なく動いてはいるのですが、今後列数が増えていくと冗長なif文の繰り返しになってしまうので、これを配列化し最適化したいと考えております。
|
@@ -16,7 +16,19 @@
|
|
16
16
|
<td>4</td>
|
17
17
|
<td>5</td>
|
18
18
|
</tr>
|
19
|
+
</tbody>
|
20
|
+
</table>
|
21
|
+
|
22
|
+
<table id="targetTable">
|
23
|
+
<tbody>
|
19
24
|
<tr>
|
25
|
+
<td>1</td>
|
26
|
+
<td>2</td>
|
27
|
+
<td>3</td>
|
28
|
+
<td>4</td>
|
29
|
+
<td>5</td>
|
30
|
+
</tr>
|
31
|
+
<tr>
|
20
32
|
<td></td>
|
21
33
|
<td></td>
|
22
34
|
<td></td>
|
@@ -58,19 +70,20 @@
|
|
58
70
|
```js
|
59
71
|
$(function() {
|
60
72
|
var selected_td = $("#table td");
|
73
|
+
var target_td = $("#targetTable td");
|
61
74
|
$(selected_td).on("click", function() {
|
62
|
-
var nthItem =
|
75
|
+
var nthItem = $("#table td").index(this);
|
63
|
-
|
76
|
+
target_td.removeClass("is-selected");
|
64
77
|
if (nthItem === 0) {
|
65
|
-
$("#
|
78
|
+
$("#targetTable td:nth-child(1)").addClass("is-selected");
|
66
79
|
} else if (nthItem === 1) {
|
67
|
-
$("#
|
80
|
+
$("#targetTable td:nth-child(2)").addClass("is-selected");
|
68
81
|
} else if (nthItem === 2) {
|
69
|
-
$("#
|
82
|
+
$("#targetTable td:nth-child(3)").addClass("is-selected");
|
70
83
|
} else if (nthItem === 3) {
|
71
|
-
$("#
|
84
|
+
$("#targetTable td:nth-child(4)").addClass("is-selected");
|
72
85
|
} else if (nthItem === 4) {
|
73
|
-
$("#
|
86
|
+
$("#targetTable td:nth-child(5)").addClass("is-selected");
|
74
87
|
}
|
75
88
|
});
|
76
89
|
});
|
@@ -78,6 +91,5 @@
|
|
78
91
|
```
|
79
92
|
|
80
93
|
### サンプル
|
81
|
-
[テーブル一つ](https://jsfiddle.net/190zk4k3/2/)
|
82
94
|
[テーブル二つ](https://jsfiddle.net/190zk4k3/3/)
|
83
95
|
どうぞよろしくお願いいたします。
|
3
テーブル二つの場合を追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -78,6 +78,6 @@
|
|
78
78
|
```
|
79
79
|
|
80
80
|
### サンプル
|
81
|
-
[
|
81
|
+
[テーブル一つ](https://jsfiddle.net/190zk4k3/2/)
|
82
|
-
|
82
|
+
[テーブル二つ](https://jsfiddle.net/190zk4k3/3/)
|
83
83
|
どうぞよろしくお願いいたします。
|
2
タイトルを変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
冗長なif
|
1
|
+
冗長なif文を配列化したい
|
body
CHANGED
File without changes
|
1
コード修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -59,7 +59,7 @@
|
|
59
59
|
$(function() {
|
60
60
|
var selected_td = $("#table td");
|
61
61
|
$(selected_td).on("click", function() {
|
62
|
-
var nthItem =
|
62
|
+
var nthItem = selected_td.index(this);
|
63
63
|
selected_td.removeClass("is-selected");
|
64
64
|
if (nthItem === 0) {
|
65
65
|
$("#table td:nth-child(1)").addClass("is-selected");
|