質問編集履歴
1
コード、文章など
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,14 +1,404 @@
|
|
1
|
-
javaScript ### 加算していく
|
1
|
+
javaScript ### 数値を加算していく
|
2
2
|
|
3
3
|
|
4
|
-
forで動的に作ったIDを順番に取得して 値(set_num)は数値ですのでそれを加算していきたい。
|
5
|
-
|
4
|
+
見にくいコードですみません。
|
6
5
|
|
6
|
+
練習でjavaScriptやってます。
|
7
7
|
|
8
|
+
javaScriptのコード最後のwindow.onload時に、
|
9
|
+
forで動的に作ったID(110行目あたり)のついた値(localStrageに保存した値(値は(set_num)数値)を取得して、それを月毎合計値を最終的には 表示したいです。
|
8
10
|
|
9
|
-
方法
|
11
|
+
日にちに保存した数値の合計方法がわかりません。
|
10
12
|
|
13
|
+
よろしくお願いします。
|
14
|
+
|
15
|
+
|
16
|
+
```HTML
|
17
|
+
<!DOCTYPE html>
|
18
|
+
<html>
|
19
|
+
|
20
|
+
<head>
|
21
|
+
<meta charset="utf-8"/>
|
22
|
+
<link rel="stylesheet" type="text/css" href="style3.css">
|
23
|
+
<script language="javascript" type="text/javascript"></script>
|
24
|
+
</head>
|
25
|
+
|
26
|
+
<body>
|
27
|
+
<button id="prev" type="button">前の月</button>
|
28
|
+
<button id="next" type="button">次の月</button>
|
29
|
+
<div id = "calendar"></div>
|
30
|
+
<script src="main3.js"></script>
|
31
|
+
</body>
|
32
|
+
|
33
|
+
</html>
|
34
|
+
```
|
35
|
+
|
36
|
+
```css
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
#calendar {
|
42
|
+
height: 600px;
|
43
|
+
|
44
|
+
margin: 100px 0px 100px 0px;
|
45
|
+
}
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
h2 {
|
52
|
+
font-size: 50px;
|
53
|
+
text-align: center;
|
54
|
+
color: skyblue;
|
55
|
+
}
|
56
|
+
|
57
|
+
table {
|
58
|
+
border-spacing: 0;
|
59
|
+
border-collapse: collapse;
|
60
|
+
margin-left: 100px;
|
61
|
+
margin-right: 100px;
|
62
|
+
}
|
63
|
+
|
64
|
+
td {
|
65
|
+
border: 1px solid #ddd;
|
66
|
+
padding: 10px;
|
67
|
+
font-size: 40px;
|
68
|
+
text-align: center;
|
69
|
+
}
|
70
|
+
|
71
|
+
td:first-child {
|
72
|
+
color: red;
|
73
|
+
}
|
74
|
+
|
75
|
+
td:last-child {
|
76
|
+
color: royalblue;
|
77
|
+
}
|
78
|
+
|
79
|
+
td.is-disabled {
|
80
|
+
color: #ccc;
|
81
|
+
}
|
82
|
+
|
83
|
+
|
84
|
+
.is-today {
|
85
|
+
color: red;
|
86
|
+
}
|
87
|
+
|
88
|
+
|
89
|
+
.item {
|
90
|
+
background-color: #8ad3e3b3;
|
91
|
+
}
|
92
|
+
|
93
|
+
button#prev,#next {
|
94
|
+
font-size: 60px;
|
95
|
+
margin-right: 200px;
|
96
|
+
margin-top: 100px;
|
97
|
+
text-align: center;
|
98
|
+
color: #7a7f8b;
|
99
|
+
}
|
100
|
+
|
101
|
+
input {
|
102
|
+
width: 200px;
|
103
|
+
height: 60px;
|
104
|
+
margin-bottom: 10px;
|
105
|
+
text-align: center;
|
106
|
+
font-size: 40px;
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
p {
|
111
|
+
font-size: 50px;
|
112
|
+
margin-left: 80%;
|
113
|
+
}
|
114
|
+
```
|
115
|
+
|
11
116
|
```javaScript
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
const weeks = ['日', '月', '火', '水', '木', '金', '土']
|
122
|
+
const date = new Date(); //今日の日付
|
123
|
+
let year = date.getFullYear();
|
124
|
+
//console.log(year)
|
125
|
+
let month = date.getMonth() + 1; //10(返される値)+1
|
126
|
+
//console.log(month)
|
127
|
+
|
128
|
+
const config = {
|
129
|
+
show: 3,
|
130
|
+
};
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
function showCalendar(year, month) {
|
136
|
+
for ( i = 0; i < config.show; i++) {
|
137
|
+
|
138
|
+
const calendarHtml = createCalendar(year, month)
|
139
|
+
const sec = document.createElement('section')
|
140
|
+
|
141
|
+
sec.innerHTML = calendarHtml
|
142
|
+
document.querySelector('#calendar').appendChild(sec)
|
143
|
+
month++
|
144
|
+
if (month > 12) {
|
145
|
+
year++
|
146
|
+
month = 1
|
147
|
+
}
|
148
|
+
}
|
149
|
+
};
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
//---------------------------------------------------------------------
|
161
|
+
function createCalendar(year, month) {
|
162
|
+
|
163
|
+
const startDate = new Date(year, month - 1, 1)
|
164
|
+
const endDate = new Date(year, month, 0) // 月の最後の日を取得
|
165
|
+
const endDayCount = endDate.getDate() // 月の末日
|
166
|
+
let year1 = date.getFullYear();
|
167
|
+
let now = new Date();
|
168
|
+
const todaymonth = now.getMonth() + 1;
|
169
|
+
const lastMonthEndDate = new Date(year,month - 1,0) // 前月の最後の日の情報
|
170
|
+
const lastMonthendDayCount = lastMonthEndDate.getDate() // 前月の末日
|
171
|
+
const startDay = startDate.getDay() // 月の最初の日の曜日を取得
|
172
|
+
|
173
|
+
let dayCount = 1 // 日にちのカウント
|
174
|
+
let calendarHtml = '' // HTMLを組み立てる変数
|
175
|
+
|
176
|
+
|
177
|
+
calendarHtml += '<h2>' + year + '/' + month + '</h2>'
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
calendarHtml += '<table id=month_id'+ year + month + '>'+ '<p id=' + year + month + '_sum>' + '合計' + '</p>' + '<tbody>'
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
// 曜日の行を作成
|
186
|
+
for (let i = 0; i < weeks.length; i++) {
|
187
|
+
|
188
|
+
calendarHtml += '<td>' + weeks[i] + '</td>'
|
189
|
+
};
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
//ここからカレンダー作成
|
195
|
+
//---------------------------------------------------------------------
|
196
|
+
for (let w = 0; w < 6; w++) {
|
197
|
+
|
198
|
+
calendarHtml += '<tr>'
|
199
|
+
|
200
|
+
for (let d = 0; d < 7; d++) { //0~6まで インクリメント 7日で繰り返す
|
201
|
+
|
202
|
+
if (w == 0 && d < startDay) { //wが0かつ5より小さいとき空
|
203
|
+
// 1行目で1日の曜日の前
|
204
|
+
|
205
|
+
let num = lastMonthendDayCount - startDay + d + 1
|
206
|
+
|
207
|
+
calendarHtml += '<td class="is-disabled">' + num + '</td>'
|
208
|
+
|
209
|
+
|
210
|
+
} else if (dayCount > endDayCount) {
|
211
|
+
// 末尾の日数を超えた
|
212
|
+
|
213
|
+
|
214
|
+
let num = dayCount - endDayCount
|
215
|
+
calendarHtml += '<td class="is-disabled">' + num + '</td>'
|
216
|
+
dayCount++
|
217
|
+
|
218
|
+
} else {
|
219
|
+
let year1 = date.getFullYear();
|
220
|
+
const startDate2 = new Date(year, month - 1, 1)
|
221
|
+
const startDay2 = startDate2.getDay() // 月の最初の日の曜日を取得
|
222
|
+
const todays2 = date.getDate();
|
223
|
+
let today_month2 = date.getMonth() + 1;
|
224
|
+
let today_c2 = (6+todays2 + startDay2)
|
225
|
+
|
226
|
+
calendarHtml += `<td id= `+ `"` + year1 + today_month2 + dayCount + `"` + ` ` + `class="calendar_td"` +
|
227
|
+
`daylength=${dayCount}>${dayCount}`+
|
228
|
+
'<br>'
|
229
|
+
+ '<input id="' + year1 + todaymonth + dayCount +'day_save"' + 'type = "text" name="" value=""' + ' >' +
|
230
|
+
'<br/> '+
|
231
|
+
'<input id="' + year1 + todaymonth + dayCount +'dayoverTime"' + 'type = "number" name="in" step="0.5" value=""' + ' >' +
|
232
|
+
'<br/> '
|
233
|
+
+' <input id="' + year1 + todaymonth + dayCount +'dayoverTime"'+' onclick="load_click(this.id);" class="btn1" type="button" value="読込" onclick="load();"/>'+
|
234
|
+
' <input id="' + year1 + todaymonth + dayCount +'day"'+' class="btn2" type="button" value="保存" onclick="reply_click(this.id);save()"/>'+
|
235
|
+
' <br/><br/>'+
|
236
|
+
' <button type="button" class="btn btn-primary btn-block" id="' + year1 + todaymonth + dayCount +'dayoverTime"' + 'onclick="reset_click(this.id);">リセット</button>' + '</td>'
|
237
|
+
dayCount++
|
238
|
+
}
|
239
|
+
}
|
240
|
+
calendarHtml += '</tr>'
|
241
|
+
};
|
242
|
+
|
243
|
+
//ここまでカレンダー作成
|
244
|
+
//---------------------------------------------------------------------
|
245
|
+
|
246
|
+
const todays = date.getDate();
|
247
|
+
let today_month = date.getMonth() + 1;
|
248
|
+
let today_c = (5+todays+startDay)
|
249
|
+
const year_index = '<h2>' + year + '/' + (month-1) + '</h2>'
|
250
|
+
|
251
|
+
|
252
|
+
if(year_index == '<h2>' + year + '/' + today_month + '<h2>' ) {
|
253
|
+
|
254
|
+
let cl = document.getElementsByTagName("td");
|
255
|
+
cl[today_c].classList.add('item');
|
256
|
+
};
|
257
|
+
//---------------------------------------------------------------------
|
258
|
+
|
259
|
+
|
260
|
+
calendarHtml += '</tbody>' + '</table>'
|
261
|
+
|
262
|
+
return calendarHtml //ここまでの処理結果を下のshowCalendarに返す
|
263
|
+
|
264
|
+
};
|
265
|
+
//---------------------------------------------------------------------
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
function moveCalendar(e) {
|
272
|
+
document.querySelector('#calendar').innerHTML = ''
|
273
|
+
|
274
|
+
if (e.target.id === 'prev') {
|
275
|
+
month--
|
276
|
+
|
277
|
+
if (month < 1) {
|
278
|
+
year--
|
279
|
+
month = 12
|
280
|
+
}
|
281
|
+
}
|
282
|
+
|
283
|
+
if (e.target.id === 'next') {
|
284
|
+
month++
|
285
|
+
|
286
|
+
if (month > 12) {
|
287
|
+
year++
|
288
|
+
month = 1
|
289
|
+
}
|
290
|
+
}
|
291
|
+
|
292
|
+
showCalendar(year, month)
|
293
|
+
};
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
|
300
|
+
document.querySelector('#prev').addEventListener('click', moveCalendar)
|
301
|
+
document.querySelector('#next').addEventListener('click', moveCalendar)
|
302
|
+
|
303
|
+
showCalendar(year, month);
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
|
312
|
+
window.onload = function (){
|
313
|
+
let ret = document.getElementById("month_id"+ year1 +month)
|
314
|
+
.getElementsByTagName("td")
|
315
|
+
[today_c2].classList.add("item");
|
316
|
+
};
|
317
|
+
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
// 保存 id 201911day
|
322
|
+
function reply_click(clicked_id)
|
323
|
+
{
|
324
|
+
//alert(clicked_id);
|
325
|
+
let num_day = (clicked_id);
|
326
|
+
|
327
|
+
console.log(num_day);
|
328
|
+
|
329
|
+
save(num_day);
|
330
|
+
|
331
|
+
};
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
// 保存
|
336
|
+
function save(num_day) {
|
337
|
+
|
338
|
+
let mydata = document.getElementById(num_day+'overTime').value;
|
339
|
+
document.getElementById(num_day+'_save').value = mydata
|
340
|
+
console.log(mydata);
|
341
|
+
localStorage.setItem(num_day+'overTime', mydata);
|
342
|
+
|
343
|
+
};
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
// 読込 id 201911dayoverTime
|
350
|
+
function load_click(clicked_id){
|
351
|
+
//alert(clicked_id);
|
352
|
+
let load_data = (clicked_id);
|
353
|
+
load(load_data);
|
354
|
+
};
|
355
|
+
|
356
|
+
|
357
|
+
|
358
|
+
// 読込
|
359
|
+
|
360
|
+
function load(load_data) {
|
361
|
+
let memo = 'メモ書きは,';
|
362
|
+
let mydata = "";
|
363
|
+
|
364
|
+
|
365
|
+
if (!localStorage.getItem(load_data)) {
|
366
|
+
mydata = "データがありません";
|
367
|
+
console.log(`mydata= ${mydata}`);
|
368
|
+
} else {
|
369
|
+
mydata = load_data + '=' + localStorage.getItem(load_data);
|
370
|
+
console.log(`mydata= ${mydata}`);
|
371
|
+
var str = mydata;
|
372
|
+
console.log(str);
|
373
|
+
|
374
|
+
}
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
//リセット
|
382
|
+
|
383
|
+
function reset_click(clicked_id){
|
384
|
+
console.log(clicked_id);
|
385
|
+
|
386
|
+
if (!localStorage.getItem(clicked_id)) {
|
387
|
+
r_mydata = "データがありません";
|
388
|
+
console.log(r_mydata);
|
389
|
+
} else {
|
390
|
+
r_mydata = clicked_id + '=' + localStorage.getItem(clicked_id);
|
391
|
+
console.log(r_mydata);
|
392
|
+
localStorage.removeItem(clicked_id,r_mydata);
|
393
|
+
console.log(clicked_id);
|
394
|
+
}
|
395
|
+
}
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
|
12
402
|
window.onload = function() {
|
13
403
|
const date = new Date(); //今日の日付
|
14
404
|
const year = date.getFullYear();
|
@@ -18,24 +408,14 @@
|
|
18
408
|
const d = new Date(year, month, 0).getDate();
|
19
409
|
console.log(d)
|
20
410
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
411
|
for (let len = 0; len < d+1; len++){
|
412
|
+
var overvalue = (localStorage.getItem(year.toString()+month.toString()+len.toString()+'dayoverTime'));
|
413
|
+
var set_num = Number(overvalue);
|
414
|
+
}
|
25
415
|
|
26
|
-
|
416
|
+
const element_sum = document.getElementById('201911_sum');
|
417
|
+
element_sum.innerHTML = set_num;
|
27
418
|
|
28
|
-
|
419
|
+
};
|
29
420
|
|
30
|
-
|
31
|
-
//ここで値を加算してみたが これだと意味ないですが他に思いつきません
|
32
|
-
if (!isNaN(set_num)){
|
33
|
-
set_num += set_num;
|
34
|
-
console.log(set_num)
|
35
|
-
};
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
}
|
40
|
-
};
|
41
421
|
```
|