回答編集履歴

2

答えのjsコードを編集削除

2018/12/24 08:02

投稿

m.ts10806
m.ts10806

スコア80852

test CHANGED
@@ -40,32 +40,6 @@
40
40
 
41
41
 
42
42
 
43
- ```js
43
+ ループさせなくてもnotとか使えば「この番号以外」という指定は1行で可能ですね。
44
44
 
45
- $(function () {
46
-
47
- $(".button").on("click",function () {
48
-
49
- $(this).next().slideToggle().siblings(".content");
50
-
51
- contentClose($(".button").index(this));
52
-
53
- });
54
-
55
- });
56
-
57
- function contentClose(contents_index){
45
+ そこは調べてやってみてください。たぶんループでやるより高速です。
58
-
59
- $(".content").each(function(){
60
-
61
- if(contents_index != $(".content").index(this)){
62
-
63
- $(this).hide();
64
-
65
- }
66
-
67
- });
68
-
69
- }
70
-
71
- ```

1

追記

2018/12/24 08:01

投稿

m.ts10806
m.ts10806

スコア80852

test CHANGED
@@ -1 +1,71 @@
1
1
  [.contentをループ](https://www.task-notes.com/entry/20140725/1406216933)で回し、[表示か非表示かを判定](http://www.koikikukan.com/archives/2012/08/03-012345.php)して表示だったら非表示にすれば良いのではないでしょうか。
2
+
3
+
4
+
5
+ ヒントサンプルコード:
6
+
7
+ ```css
8
+
9
+ .contentを非表示
10
+
11
+ ```
12
+
13
+ ```js
14
+
15
+ $(function () {
16
+
17
+ $(".button").on("click",function () {
18
+
19
+ $(this).next().slideToggle().siblings(".content");
20
+
21
+ 何番目のボタンが押されたかを取得してその番号を引数にcontentを閉じる用のユーザ関数に飛ばす
22
+
23
+ });
24
+
25
+ });
26
+
27
+
28
+
29
+ .contentを閉じる用のユーザ関数 引数:何番目のボタンが押されたか
30
+
31
+ .contentをループ
32
+
33
+ 自分の番号以外
34
+
35
+ $(this).hide();
36
+
37
+
38
+
39
+ ```
40
+
41
+
42
+
43
+ ```js
44
+
45
+ $(function () {
46
+
47
+ $(".button").on("click",function () {
48
+
49
+ $(this).next().slideToggle().siblings(".content");
50
+
51
+ contentClose($(".button").index(this));
52
+
53
+ });
54
+
55
+ });
56
+
57
+ function contentClose(contents_index){
58
+
59
+ $(".content").each(function(){
60
+
61
+ if(contents_index != $(".content").index(this)){
62
+
63
+ $(this).hide();
64
+
65
+ }
66
+
67
+ });
68
+
69
+ }
70
+
71
+ ```