質問編集履歴

1

試したことを追記しました

2018/05/22 07:49

投稿

yuzuki-chan
yuzuki-chan

スコア8

test CHANGED
File without changes
test CHANGED
@@ -90,10 +90,84 @@
90
90
 
91
91
  ### 試したこと
92
92
 
93
+ ```javascript
94
+
95
+ $(function(){
96
+
97
+ //divとulそれぞれにidを追加
98
+
99
+ $('.group_name , .list').each(function(i){
100
+
101
+ $(this).attr('id','child' + (i+1));
102
+
103
+ });
104
+
105
+ //id指定で要素をつつみこむ(ここ、規則性あるけど別の書き方はできないのでしょうか?)
106
+
107
+ $("#child1,#child2").wrapAll('<div id="group1" />');
108
+
109
+ $("#child3,#child4").wrapAll('<div id="group2" />');
110
+
111
+    ・・・
112
+
113
+ });
114
+
115
+ ```
116
+
117
+ ↓実行結果
93
118
 
94
119
 
95
- wrapAll()を使って1つ1つにidを与えてid指定で囲って〜とか考えて調べてましたが
96
120
 
97
- もっとスマートな方法はないか?とおもい皆様のお知恵を借りようと思いました。
121
+ ```HTML
98
122
 
123
+ <div>
124
+
125
+ <div class="group1">
126
+
127
+ <div class="group_name" id="child1">グループ1</div>
128
+
129
+ <ul class="list" id="child2">
130
+
131
+ <li>リスト1</li>
132
+
133
+ <li>リスト2</li>
134
+
135
+ </ul>
136
+
137
+ </div>
138
+
139
+ <div class="group2">
140
+
141
+ <div class="group_name" id="child3">グループ2</div>
142
+
143
+ <ul class="list" id="child4">
144
+
145
+ <li>リスト1</li>
146
+
147
+ <li>リスト2</li>
148
+
149
+ </ul>
150
+
151
+ </div>
152
+
153
+ (上記を繰り返し)
154
+
155
+ </div>
156
+
157
+ ```
158
+
159
+
160
+
161
+ 上記の方法で一応形にはなるのですが、
162
+
163
+
164
+
165
+ ・wrapAll()の部分は規則性があるのに手打ち(別の書き方はないのか?)
166
+
167
+ ・そもそも子要素1つ1つにidを与えなくてもいい書き方はないのか?
168
+
169
+
170
+
99
- ろしくお願致します。
171
+ 以上のうな疑問を抱ています。
172
+
173
+ 不慣れなもので拙い部分もあるかと思いますが、ご回答いただけると嬉しいです!