質問編集履歴
3
現在の進捗を追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,5 +1,41 @@
|
|
1
1
|
現在、改善箇所にはテンプレートリテラルを使っていいるのですが、使わずHTMLに表示する方法を教えていただきたいです。
|
2
2
|
|
3
|
+
```
|
4
|
+
|
5
|
+
↓現在このように進めているのですがなかなかうまくいきません。
|
6
|
+
|
7
|
+
$(".message").remove();
|
8
|
+
|
9
|
+
$.each(result, function(index, item) {
|
10
|
+
|
11
|
+
var template = '<li class="lists__item">' +
|
12
|
+
|
13
|
+
'<div class="lists__item__inner">' +
|
14
|
+
|
15
|
+
'<a href=" ' + item.Item.itemUrl + ' " class="lists__item__link" target="_blank">' +
|
16
|
+
|
17
|
+
'<img src=" ' + item.Item.largeImageUrl + ' " class="lists__item__img" alt=" ' + item.Item.title + ' ">' +
|
18
|
+
|
19
|
+
'<p class="lists__item__detail">作品名:' + item.Item.title + '</p>' +
|
20
|
+
|
21
|
+
'<p class="lists__item__detail">作者 :' + item.Item.author + '</p>' +
|
22
|
+
|
23
|
+
'<p class="lists__item__detail">出版社:' + item.Item.publisherName + '</p>' +
|
24
|
+
|
25
|
+
'</a>' +
|
26
|
+
|
27
|
+
' </div>' +
|
28
|
+
|
29
|
+
'</li>';
|
30
|
+
|
31
|
+
});
|
32
|
+
|
33
|
+
$('.lists').append(template);
|
34
|
+
|
35
|
+
}
|
36
|
+
|
37
|
+
```
|
38
|
+
|
3
39
|
|
4
40
|
|
5
41
|
↓改善したい箇所
|
2
修正箇所をより詳しくしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,7 +6,57 @@
|
|
6
6
|
|
7
7
|
```
|
8
8
|
|
9
|
+
↓一部
|
10
|
+
|
11
|
+
template += `<li class='lists__item'>
|
12
|
+
|
13
|
+
<div class='lists__item__inner'>
|
14
|
+
|
15
|
+
<a href='${item.Item.itemUrl}' class='lists__item__link' target='_blank'>
|
16
|
+
|
17
|
+
<img src='${item.Item.largeImageUrl}' class='lists__item__img' alt='${item.Item.title}'>
|
18
|
+
|
19
|
+
<p class='lists__item__detail'>作品名:${item.Item.title}</p>
|
20
|
+
|
21
|
+
<p class='lists__item__detail'>作者 :${item.Item.author}</p>
|
22
|
+
|
23
|
+
<p class='lists__item__detail'>出版社:${item.Item.publisherName}</p>
|
24
|
+
|
25
|
+
</a>
|
26
|
+
|
27
|
+
</div>
|
28
|
+
|
29
|
+
</li>`;
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
var pager = `<div class="js-pager">
|
34
|
+
|
35
|
+
<ul class="btn-wrapper">
|
36
|
+
|
37
|
+
<li class="prev btn-item">
|
38
|
+
|
39
|
+
<a class="btn-anchor" href="#">前へ</a>
|
40
|
+
|
41
|
+
</li>
|
42
|
+
|
43
|
+
<li class="next btn-item">
|
44
|
+
|
45
|
+
<a class="btn-anchor" href="#">次へ</a>
|
46
|
+
|
47
|
+
</li>
|
48
|
+
|
49
|
+
</ul>
|
50
|
+
|
51
|
+
</div>`;
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
↓全体
|
58
|
+
|
9
|
-
unction showResult(result) {
|
59
|
+
function showResult(result) {
|
10
60
|
|
11
61
|
$('.message').remove();
|
12
62
|
|
1
コード全体を載せました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,41 +1,379 @@
|
|
1
|
-
|
1
|
+
現在、改善箇所にはテンプレートリテラルを使っていいるのですが、使わずHTMLに表示する方法を教えていただきたいです。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
↓改善したい箇所
|
2
6
|
|
3
7
|
```
|
4
8
|
|
9
|
+
unction showResult(result) {
|
10
|
+
|
11
|
+
$('.message').remove();
|
12
|
+
|
13
|
+
var template= '';
|
14
|
+
|
15
|
+
$.each(result, function(index, item) {
|
16
|
+
|
5
|
-
<li class='lists__item'>
|
17
|
+
template += `<li class='lists__item'>
|
6
|
-
|
18
|
+
|
7
|
-
|
19
|
+
<div class='lists__item__inner'>
|
8
|
-
|
20
|
+
|
9
|
-
|
21
|
+
<a href='${item.Item.itemUrl}' class='lists__item__link' target='_blank'>
|
10
|
-
|
22
|
+
|
11
|
-
|
23
|
+
<img src='${item.Item.largeImageUrl}' class='lists__item__img' alt='${item.Item.title}'>
|
12
|
-
|
24
|
+
|
13
|
-
<p class='lists__item__detail'>作品名:
|
25
|
+
<p class='lists__item__detail'>作品名:${item.Item.title}</p>
|
14
|
-
|
26
|
+
|
15
|
-
<p class='lists__item__detail'>作者 :
|
27
|
+
<p class='lists__item__detail'>作者 :${item.Item.author}</p>
|
16
|
-
|
28
|
+
|
17
|
-
<p class='lists__item__detail'>出版社:
|
29
|
+
<p class='lists__item__detail'>出版社:${item.Item.publisherName}</p>
|
18
|
-
|
30
|
+
|
19
|
-
</a>
|
31
|
+
</a>
|
20
|
-
|
32
|
+
|
21
|
-
</div>
|
33
|
+
</div>
|
34
|
+
|
22
|
-
|
35
|
+
</li>`;
|
36
|
+
|
37
|
+
});
|
38
|
+
|
39
|
+
$('.lists').html(template);
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
function showPager(result) {
|
46
|
+
|
47
|
+
$('.js-pager').remove();
|
48
|
+
|
49
|
+
$('.prev, .next').removeClass('disabled');
|
50
|
+
|
51
|
+
var pager = `<div class="js-pager">
|
52
|
+
|
53
|
+
<ul class="btn-wrapper">
|
54
|
+
|
55
|
+
<li class="prev btn-item">
|
56
|
+
|
57
|
+
<a class="btn-anchor" href="#">前へ</a>
|
58
|
+
|
23
|
-
</li>
|
59
|
+
</li>
|
60
|
+
|
61
|
+
<li class="next btn-item">
|
62
|
+
|
63
|
+
<a class="btn-anchor" href="#">次へ</a>
|
64
|
+
|
65
|
+
</li>
|
66
|
+
|
67
|
+
</ul>
|
68
|
+
|
69
|
+
</div>`;
|
24
70
|
|
25
71
|
```
|
26
72
|
|
73
|
+
```HTML
|
74
|
+
|
75
|
+
<!doctype html>
|
76
|
+
|
77
|
+
<html>
|
78
|
+
|
79
|
+
<head>
|
80
|
+
|
81
|
+
<meta charset="UTF-8">
|
82
|
+
|
83
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
84
|
+
|
85
|
+
<meta name="format-detection" content="telephone=no">
|
86
|
+
|
87
|
+
<title>jQuery</title>
|
88
|
+
|
89
|
+
<link rel="stylesheet" href="../../common/css/reset.css">
|
90
|
+
|
91
|
+
<link rel="stylesheet" href="../../common/css/base.css">
|
92
|
+
|
93
|
+
<link rel="stylesheet" href="./css/style.css?20200406">
|
94
|
+
|
95
|
+
</head>
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
<body>
|
102
|
+
|
27
|
-
|
103
|
+
<div class="wrap">
|
104
|
+
|
105
|
+
<div class="container">
|
106
|
+
|
107
|
+
<div class="header">
|
108
|
+
|
109
|
+
<p class="header__title">Search Books!</p>
|
110
|
+
|
111
|
+
</div>
|
112
|
+
|
113
|
+
<div class="search">
|
114
|
+
|
115
|
+
<div class="search__text">
|
116
|
+
|
117
|
+
<input type="text" id="js-search-word" class="search__text__input" placeholder="検索する">
|
118
|
+
|
119
|
+
</div>
|
120
|
+
|
121
|
+
<button id="js-search-button" class="search__btn">検索する</button>
|
122
|
+
|
123
|
+
</div>
|
124
|
+
|
125
|
+
<ul class="lists"></ul>
|
126
|
+
|
127
|
+
</div>
|
128
|
+
|
129
|
+
</div>
|
130
|
+
|
131
|
+
</div>
|
132
|
+
|
133
|
+
</body>
|
134
|
+
|
135
|
+
</html>
|
28
136
|
|
29
137
|
```
|
30
138
|
|
139
|
+
```JQuery
|
140
|
+
|
141
|
+
<script src="../../common/js/jquery.js"></script>
|
142
|
+
|
143
|
+
<script>
|
144
|
+
|
145
|
+
$(function() {
|
146
|
+
|
147
|
+
var parameter = {
|
148
|
+
|
149
|
+
applicationId: '1013041242368735405',
|
150
|
+
|
151
|
+
keyword: '',
|
152
|
+
|
153
|
+
hits: 20,
|
154
|
+
|
155
|
+
page: 1,
|
156
|
+
|
157
|
+
booksGenreId: '001',
|
158
|
+
|
159
|
+
};
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
var isLastPage = false;
|
164
|
+
|
165
|
+
$('.search__btn').on('click', function() {
|
166
|
+
|
167
|
+
init();
|
168
|
+
|
169
|
+
request();
|
170
|
+
|
171
|
+
});
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
$(document).on('click', '.prev', function() {
|
176
|
+
|
31
|
-
|
177
|
+
if(parameter.page === 1) return false;
|
178
|
+
|
32
|
-
|
179
|
+
isLastPage = false;
|
180
|
+
|
181
|
+
parameter.page -- ;
|
182
|
+
|
183
|
+
request();
|
184
|
+
|
185
|
+
});
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
$(document).on('click', '.next', function() {
|
190
|
+
|
191
|
+
if(isLastPage) return noMorePageMessage();
|
192
|
+
|
193
|
+
parameter.page ++ ;
|
194
|
+
|
195
|
+
request();
|
196
|
+
|
197
|
+
});
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
function request() {
|
202
|
+
|
203
|
+
parameter.keyword = $('#js-search-word').val();
|
204
|
+
|
205
|
+
$.ajax ({
|
206
|
+
|
207
|
+
url:'https://app.rakuten.co.jp/services/api/BooksTotal/Search/20130522',
|
208
|
+
|
209
|
+
type: 'GET',
|
210
|
+
|
211
|
+
datatype: 'json',
|
212
|
+
|
213
|
+
data: parameter,
|
214
|
+
|
215
|
+
}).done(function(data) {
|
216
|
+
|
217
|
+
if(data.count === 0) {
|
218
|
+
|
219
|
+
notFoundMessage();
|
220
|
+
|
221
|
+
}else {
|
222
|
+
|
223
|
+
showResult(data.Items);
|
224
|
+
|
225
|
+
showPager(data);
|
226
|
+
|
227
|
+
}if (parameter.page === data.pageCount) isLastPage = true;
|
228
|
+
|
229
|
+
}).fail(function(e) {
|
230
|
+
|
231
|
+
responseErrorMessage(e);
|
232
|
+
|
233
|
+
});
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
function init() {
|
240
|
+
|
241
|
+
parameter.page = 1;
|
242
|
+
|
243
|
+
isLastPage = false;
|
244
|
+
|
245
|
+
$('.message').remove();
|
246
|
+
|
247
|
+
$('.lists').empty();
|
248
|
+
|
249
|
+
};
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
function notFoundMessage() {
|
254
|
+
|
255
|
+
$('.message').remove();
|
256
|
+
|
257
|
+
$('.lists').before('<div class="message">検索結果が見つかりませんでした。<br>別のキーワードで検索して下さい。</div>');
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
function noMorePageMessage() {
|
264
|
+
|
265
|
+
$('.message').remove();
|
266
|
+
|
267
|
+
$('.lists').before('<div class="message">これ以上はありません</div>');
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
|
272
|
+
|
33
|
-
|
273
|
+
function responseErrorMessage(e) {
|
274
|
+
|
34
|
-
|
275
|
+
$('.message').remove();
|
276
|
+
|
277
|
+
if (e.status === 0) {
|
278
|
+
|
279
|
+
$('.lists').before('<div class="message">通信に失敗しました。インターネットの接続をご確認ください。</div>');
|
280
|
+
|
35
|
-
|
281
|
+
}else if (e.status === 400) {
|
282
|
+
|
36
|
-
|
283
|
+
$('.lists').before('<div class="message">検索文字が空、もしくは半角英数1文字になっています。</div>');
|
284
|
+
|
37
|
-
|
285
|
+
}else if (e.status === 429) {
|
286
|
+
|
38
|
-
|
287
|
+
$('.lists').before('<div class="message">リクエスト過多です。<br>しばらく時間を空けてから、ご利用ください。</div>');
|
288
|
+
|
289
|
+
}else {
|
290
|
+
|
291
|
+
$('.lists').before('<div class="message">予期せぬエラーが発生しました.</div>');
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
function showResult(result) {
|
300
|
+
|
301
|
+
$('.message').remove();
|
302
|
+
|
303
|
+
var template= '';
|
304
|
+
|
305
|
+
$.each(result, function(index, item) {
|
306
|
+
|
307
|
+
template += `<li class='lists__item'>
|
308
|
+
|
309
|
+
<div class='lists__item__inner'>
|
310
|
+
|
311
|
+
<a href='${item.Item.itemUrl}' class='lists__item__link' target='_blank'>
|
312
|
+
|
313
|
+
<img src='${item.Item.largeImageUrl}' class='lists__item__img' alt='${item.Item.title}'>
|
314
|
+
|
315
|
+
<p class='lists__item__detail'>作品名:${item.Item.title}</p>
|
316
|
+
|
317
|
+
<p class='lists__item__detail'>作者 :${item.Item.author}</p>
|
318
|
+
|
319
|
+
<p class='lists__item__detail'>出版社:${item.Item.publisherName}</p>
|
320
|
+
|
321
|
+
</a>
|
322
|
+
|
323
|
+
</div>
|
324
|
+
|
325
|
+
</li>`;
|
326
|
+
|
327
|
+
});
|
328
|
+
|
329
|
+
$('.lists').html(template);
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
function showPager(result) {
|
336
|
+
|
337
|
+
$('.js-pager').remove();
|
338
|
+
|
339
|
+
$('.prev, .next').removeClass('disabled');
|
340
|
+
|
341
|
+
var pager = `<div class="js-pager">
|
342
|
+
|
39
|
-
|
343
|
+
<ul class="btn-wrapper">
|
344
|
+
|
345
|
+
<li class="prev btn-item">
|
346
|
+
|
347
|
+
<a class="btn-anchor" href="#">前へ</a>
|
348
|
+
|
349
|
+
</li>
|
350
|
+
|
351
|
+
<li class="next btn-item">
|
352
|
+
|
353
|
+
<a class="btn-anchor" href="#">次へ</a>
|
354
|
+
|
355
|
+
</li>
|
356
|
+
|
357
|
+
</ul>
|
358
|
+
|
359
|
+
</div>`;
|
360
|
+
|
361
|
+
$('.lists').after(pager);
|
362
|
+
|
363
|
+
if(result.page === 1) {
|
364
|
+
|
365
|
+
$('.prev').addClass('disabled');
|
366
|
+
|
367
|
+
}if(result.page === result.pageCount) {
|
368
|
+
|
369
|
+
$('.next').addClass('disabled');
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
});
|
376
|
+
|
377
|
+
</script>
|
40
378
|
|
41
379
|
```
|