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

回答編集履歴

1

追記

2016/09/23 05:14

投稿

yambejp
yambejp

スコア117967

answer CHANGED
@@ -13,7 +13,7 @@
13
13
  ```HTML
14
14
  <input type="checkbox" id="c1" class="oritatami">
15
15
  <h4><label for="c1">メニュー1</label></h4>
16
- <div id="o1">
16
+ <div>
17
17
  <ul>
18
18
  <li> メニュー1-1</li>
19
19
  <li> メニュー1-2</li>
@@ -22,7 +22,7 @@
22
22
  </div>
23
23
  <input type="checkbox" id="c2" class="oritatami" checked>
24
24
  <h4><label for="c2">メニュー2</label></h4>
25
- <div id="o2">
25
+ <div>
26
26
  <ul>
27
27
  <li> メニュー2-1</li>
28
28
  <li> メニュー2-2</li>
@@ -31,7 +31,7 @@
31
31
  </div>
32
32
  <input type="checkbox" id="c3" class="oritatami">
33
33
  <h4><label for="c3">メニュー3</label></h4>
34
- <div id="o3">
34
+ <div>
35
35
  <ul>
36
36
  <li> メニュー3-1</li>
37
37
  <li> メニュー3-2</li>
@@ -39,4 +39,64 @@
39
39
  </ul>
40
40
  </div>
41
41
 
42
+ ```
43
+
44
+ # 追記
45
+ :checkedが使えない場合のjQuery補足バージョンです
46
+ 古いIEはチェックボックスを非表示にすると色々問題あるので表示外に飛ばしてしまいます
47
+ ```CSS
48
+ input.oritatami {position:absolute;margin-top:-9999px;}
49
+ label.open:after{content:"↓";}
50
+ label.close:after{content:"→";}
51
+ div.open{display:block;}
52
+ div.close{display:none;}
53
+ ```
54
+ ```javascript
55
+ /* 別途jquery組み込むこと */
56
+ $(function(){
57
+ $('input.oritatami[type=checkbox]').change(function(){
58
+ if($(this).prop('checked')){
59
+ var remove="close";
60
+ var add="open";
61
+ }else{
62
+ var remove="open";
63
+ var add="close";
64
+ }
65
+ $(this).nextAll('h4').first().children('label').addClass(add);
66
+ $(this).nextAll('h4').first().children('label').removeClass(remove);
67
+ $(this).nextAll('div').first().addClass(add);
68
+ $(this).nextAll('div').first().removeClass(remove);
69
+ }).change();
70
+ });
71
+
72
+ ```
73
+ ```HTML
74
+ <input type="checkbox" id="c1" class="oritatami">
75
+ <h4><label for="c1">メニュー1</label></h4>
76
+ <div>
77
+ <ul>
78
+ <li> メニュー1-1</li>
79
+ <li> メニュー1-2</li>
80
+ <li> メニュー1-3</li>
81
+ </ul>
82
+ </div>
83
+ <input type="checkbox" id="c2" class="oritatami" checked>
84
+ <h4><label for="c2">メニュー2</label></h4>
85
+ <div>
86
+ <ul>
87
+ <li> メニュー2-1</li>
88
+ <li> メニュー2-2</li>
89
+ <li> メニュー2-3</li>
90
+ </ul>
91
+ </div>
92
+ <input type="checkbox" id="c3" class="oritatami">
93
+ <h4><label for="c3">メニュー3</label></h4>
94
+ <div>
95
+ <ul>
96
+ <li> メニュー3-1</li>
97
+ <li> メニュー3-2</li>
98
+ <li> メニュー3-3</li>
99
+ </ul>
100
+ </div>
101
+
42
102
  ```