回答編集履歴
3
要素の生成について追記
test
CHANGED
@@ -217,3 +217,63 @@
|
|
217
217
|
new Question();
|
218
218
|
|
219
219
|
```
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
# 要素の生成について
|
224
|
+
|
225
|
+
肝の部分は下記です。それ以外の部分は下記を実行する時に必要な配列(例でいうと、説明文1~3が格納されている配列)を用意するための下準備となります。
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
```javascript
|
230
|
+
|
231
|
+
var target = document.getElementById('keyword');
|
232
|
+
|
233
|
+
var df = document.createDocumentFragment();
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
/* <span id="keyword1" class="keyword">テキスト</span> の形でセットする*/
|
238
|
+
|
239
|
+
['説明文1', '説明文2', '説明文3'].forEach(function(item, index) {
|
240
|
+
|
241
|
+
var el = document.createElement('span');
|
242
|
+
|
243
|
+
el.id = "keyword" + index;
|
244
|
+
|
245
|
+
el.classList = "keyword";
|
246
|
+
|
247
|
+
el.innerText = item;
|
248
|
+
|
249
|
+
df.appendChild(el);
|
250
|
+
|
251
|
+
});
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
console.log(df);
|
256
|
+
|
257
|
+
/*
|
258
|
+
|
259
|
+
#document-fragment
|
260
|
+
|
261
|
+
<span id="keyword0" class="keyword">説明文1</span>
|
262
|
+
|
263
|
+
<span id="keyword1" class="keyword">説明文2</span>
|
264
|
+
|
265
|
+
<span id="keyword2" class="keyword">説明文3</span>
|
266
|
+
|
267
|
+
*/
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
/* 上記生成した要素をtargetである<div id="keyword"></div>内に挿入 */
|
272
|
+
|
273
|
+
target.appendChild(df);
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
```
|
278
|
+
|
279
|
+
|
2
微修正
test
CHANGED
@@ -152,7 +152,9 @@
|
|
152
152
|
|
153
153
|
|
154
154
|
|
155
|
-
/* <span id="keyword1" class="keyword">テキスト</span> の形でセットする*/
|
155
|
+
/* <span id="keyword1" class="keyword">テキスト</span> の形でセットする*/
|
156
|
+
|
157
|
+
keywordTextList.forEach(function(item, index) {
|
156
158
|
|
157
159
|
var el = document.createElement('span');
|
158
160
|
|
1
ロジック修正。ボタン追加。
test
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
何らかのイベント時(ボタンを押すなど)に`this.setQuestion()`を再度実行すれば、別の問題を出題する形となります。
|
4
4
|
|
5
|
+
サンプルとして、ランダムに出題するボタンを追加しました。
|
6
|
+
|
5
7
|
|
6
8
|
|
7
9
|
```html
|
@@ -32,6 +34,10 @@
|
|
32
34
|
|
33
35
|
</form>
|
34
36
|
|
37
|
+
|
38
|
+
|
39
|
+
<button id="next" class="btn">次の問題</button>
|
40
|
+
|
35
41
|
```
|
36
42
|
|
37
43
|
|
@@ -88,6 +94,8 @@
|
|
88
94
|
|
89
95
|
this.keywordList = document.getElementById('keyword');
|
90
96
|
|
97
|
+
this.btn = document.getElementById('next');
|
98
|
+
|
91
99
|
|
92
100
|
|
93
101
|
// キーワードのキー部分を配列として取得
|
@@ -96,43 +104,55 @@
|
|
96
104
|
|
97
105
|
this.setQuestion();
|
98
106
|
|
107
|
+
this.bindEvent();
|
108
|
+
|
109
|
+
},
|
110
|
+
|
111
|
+
bindEvent: function() {
|
112
|
+
|
113
|
+
this.btn.addEventListener('click', this.setQuestion.bind(this), false);
|
114
|
+
|
99
115
|
},
|
100
116
|
|
101
117
|
setQuestion: function() {
|
102
118
|
|
103
119
|
/* 出題+キーワード表示 */
|
104
120
|
|
121
|
+
var question = this.getRandomQuestion();
|
122
|
+
|
105
|
-
this.set
|
123
|
+
this.setText(question);
|
106
|
-
|
124
|
+
|
107
|
-
this.setKeyword();
|
125
|
+
this.setKeyword(question);
|
108
|
-
|
126
|
+
|
109
|
-
},
|
127
|
+
},
|
110
|
-
|
128
|
+
|
111
|
-
|
129
|
+
getRandomQuestion: function() {
|
112
130
|
|
113
131
|
var num = Math.floor(Math.random() * this.Q.length);
|
114
132
|
|
115
133
|
|
116
134
|
|
117
|
-
t
|
135
|
+
return this.Q[num];
|
118
|
-
|
136
|
+
|
119
|
-
},
|
137
|
+
},
|
138
|
+
|
120
|
-
|
139
|
+
setText: function(text) {
|
140
|
+
|
141
|
+
this.que.innerText = text;
|
142
|
+
|
143
|
+
},
|
144
|
+
|
121
|
-
setKeyword: function() {
|
145
|
+
setKeyword: function(question) {
|
122
|
-
|
146
|
+
|
123
|
-
var keys = this.getKeywordList(),
|
147
|
+
var keys = this.getKeywordList(question),
|
124
|
-
|
148
|
+
|
125
|
-
keywordTextList = this.createKeywordTextArr(keys, this.KEYWORD)
|
149
|
+
keywordTextList = this.createKeywordTextArr(keys, this.KEYWORD),
|
126
|
-
|
127
|
-
|
128
|
-
|
150
|
+
|
129
|
-
|
151
|
+
df = document.createDocumentFragment();
|
130
|
-
|
131
|
-
|
132
|
-
|
152
|
+
|
153
|
+
|
154
|
+
|
133
|
-
|
155
|
+
/* <span id="keyword1" class="keyword">テキスト</span> の形でセットする*/ keywordTextList.forEach(function(item, index) {
|
134
|
-
|
135
|
-
keywordTextList.forEach(function(item, index) {
|
136
156
|
|
137
157
|
var el = document.createElement('span');
|
138
158
|
|
@@ -148,27 +168,27 @@
|
|
148
168
|
|
149
169
|
|
150
170
|
|
171
|
+
this.keywordList.innerHTML = '';
|
172
|
+
|
151
173
|
this.keywordList.appendChild(df);
|
152
174
|
|
153
175
|
},
|
154
176
|
|
155
|
-
getKeywordList: function() {
|
177
|
+
getKeywordList: function(question) {
|
156
178
|
|
157
179
|
// 問題文に含まれるキーワードのみを抽出した配列を作成。
|
158
180
|
|
159
|
-
var text = this.que.innerText,
|
160
|
-
|
161
|
-
|
181
|
+
var matchArr = this.keys.map(function(item) {
|
162
|
-
|
182
|
+
|
163
|
-
|
183
|
+
var result = question.match(item) || []
|
164
|
-
|
184
|
+
|
165
|
-
|
185
|
+
return result[0];
|
166
|
-
|
186
|
+
|
167
|
-
|
187
|
+
}).filter(function(item) {
|
168
|
-
|
188
|
+
|
169
|
-
|
189
|
+
return item;
|
170
|
-
|
190
|
+
|
171
|
-
|
191
|
+
});
|
172
192
|
|
173
193
|
|
174
194
|
|