質問編集履歴

1

コード、文章など

2019/11/21 23:39

投稿

yoshi.R
yoshi.R

スコア13

test CHANGED
File without changes
test CHANGED
@@ -1,25 +1,805 @@
1
- javaScript  ### 加算していく
2
-
3
-
4
-
5
-
6
-
7
- forで動的作ったIDを順番に取得して 値(set_num)は数値ですのでそれを加算していきたい
8
-
9
- 一度 一つの配列にいれていって、最後に合計を出すなど。
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
- 方法は現状が良のか他 指摘ください。
1
+ javaScript  ### 数値を加算していく
2
+
3
+
4
+
5
+
6
+
7
+ くいコードですみません
8
+
9
+
10
+
11
+ 練習でjavaScriptやってます。
12
+
13
+
14
+
15
+ javaScriptのコード最後のwindow.onload時に、
16
+
17
+ forで動的に作ったID(110行目あたり)のつた値(localStrage保存した値(値は(set_num)数値)を取得して、そを月毎合計値を最終的には 表示したです
18
+
19
+
20
+
21
+ 日にちに保存した数値の合計方法がわかりません。
22
+
23
+
24
+
25
+ よろしくお願いします。
26
+
27
+
28
+
29
+
30
+
31
+ ```HTML
32
+
33
+ <!DOCTYPE html>
34
+
35
+ <html>
36
+
37
+
38
+
39
+ <head>
40
+
41
+ <meta charset="utf-8"/>
42
+
43
+   <link rel="stylesheet" type="text/css" href="style3.css">
44
+
45
+ <script language="javascript" type="text/javascript"></script>
46
+
47
+ </head>
48
+
49
+
50
+
51
+ <body>
52
+
53
+   <button id="prev" type="button">前の月</button>
54
+
55
+   <button id="next" type="button">次の月</button>
56
+
57
+   <div id = "calendar"></div>
58
+
59
+ <script src="main3.js"></script>
60
+
61
+ </body>
62
+
63
+
64
+
65
+ </html>
66
+
67
+ ```
68
+
69
+
70
+
71
+ ```css
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+ #calendar {
82
+
83
+ height: 600px;
84
+
85
+
86
+
87
+ margin: 100px 0px 100px 0px;
88
+
89
+ }
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+ h2 {
102
+
103
+ font-size: 50px;
104
+
105
+ text-align: center;
106
+
107
+ color: skyblue;
108
+
109
+ }
110
+
111
+
112
+
113
+ table {
114
+
115
+ border-spacing: 0;
116
+
117
+ border-collapse: collapse;
118
+
119
+ margin-left: 100px;
120
+
121
+ margin-right: 100px;
122
+
123
+ }
124
+
125
+
126
+
127
+ td {
128
+
129
+ border: 1px solid #ddd;
130
+
131
+ padding: 10px;
132
+
133
+ font-size: 40px;
134
+
135
+ text-align: center;
136
+
137
+ }
138
+
139
+
140
+
141
+ td:first-child {
142
+
143
+ color: red;
144
+
145
+ }
146
+
147
+
148
+
149
+ td:last-child {
150
+
151
+ color: royalblue;
152
+
153
+ }
154
+
155
+
156
+
157
+ td.is-disabled {
158
+
159
+ color: #ccc;
160
+
161
+ }
162
+
163
+
164
+
165
+
166
+
167
+ .is-today {
168
+
169
+ color: red;
170
+
171
+ }
172
+
173
+
174
+
175
+
176
+
177
+ .item {
178
+
179
+ background-color: #8ad3e3b3;
180
+
181
+ }
182
+
183
+
184
+
185
+ button#prev,#next {
186
+
187
+ font-size: 60px;
188
+
189
+ margin-right: 200px;
190
+
191
+ margin-top: 100px;
192
+
193
+ text-align: center;
194
+
195
+ color: #7a7f8b;
196
+
197
+ }
198
+
199
+
200
+
201
+ input {
202
+
203
+ width: 200px;
204
+
205
+ height: 60px;
206
+
207
+ margin-bottom: 10px;
208
+
209
+ text-align: center;
210
+
211
+ font-size: 40px;
212
+
213
+ }
214
+
215
+
216
+
217
+
218
+
219
+ p {
220
+
221
+ font-size: 50px;
222
+
223
+ margin-left: 80%;
224
+
225
+ }
226
+
227
+ ```
18
228
 
19
229
 
20
230
 
21
231
  ```javaScript
22
232
 
233
+
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+ const weeks = ['日', '月', '火', '水', '木', '金', '土']
242
+
243
+ const date = new Date(); //今日の日付
244
+
245
+ let year = date.getFullYear();
246
+
247
+ //console.log(year)
248
+
249
+ let month = date.getMonth() + 1; //10(返される値)+1
250
+
251
+ //console.log(month)
252
+
253
+
254
+
255
+ const config = {
256
+
257
+ show: 3,
258
+
259
+ };
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+
268
+
269
+ function showCalendar(year, month) {
270
+
271
+ for ( i = 0; i < config.show; i++) {
272
+
273
+
274
+
275
+ const calendarHtml = createCalendar(year, month)
276
+
277
+ const sec = document.createElement('section')
278
+
279
+
280
+
281
+ sec.innerHTML = calendarHtml
282
+
283
+ document.querySelector('#calendar').appendChild(sec)
284
+
285
+ month++
286
+
287
+ if (month > 12) {
288
+
289
+ year++
290
+
291
+ month = 1
292
+
293
+ }
294
+
295
+ }
296
+
297
+ };
298
+
299
+
300
+
301
+
302
+
303
+
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+
312
+
313
+
314
+
315
+
316
+
317
+
318
+
319
+ //---------------------------------------------------------------------
320
+
321
+ function createCalendar(year, month) {
322
+
323
+
324
+
325
+   const startDate = new Date(year, month - 1, 1)
326
+
327
+   const endDate = new Date(year, month, 0) // 月の最後の日を取得
328
+
329
+   const endDayCount = endDate.getDate() // 月の末日
330
+
331
+   let year1 = date.getFullYear();
332
+
333
+   let now = new Date();
334
+
335
+   const todaymonth = now.getMonth() + 1;
336
+
337
+   const lastMonthEndDate = new Date(year,month - 1,0) // 前月の最後の日の情報
338
+
339
+   const lastMonthendDayCount = lastMonthEndDate.getDate() // 前月の末日
340
+
341
+   const startDay = startDate.getDay() // 月の最初の日の曜日を取得
342
+
343
+
344
+
345
+ let dayCount = 1 // 日にちのカウント
346
+
347
+ let calendarHtml = '' // HTMLを組み立てる変数
348
+
349
+
350
+
351
+
352
+
353
+ calendarHtml += '<h2>' + year + '/' + month + '</h2>'
354
+
355
+
356
+
357
+
358
+
359
+
360
+
361
+ calendarHtml += '<table id=month_id'+ year + month + '>'+ '<p id=' + year + month + '_sum>' + '合計' + '</p>' + '<tbody>'
362
+
363
+
364
+
365
+
366
+
367
+
368
+
369
+ // 曜日の行を作成
370
+
371
+ for (let i = 0; i < weeks.length; i++) {
372
+
373
+
374
+
375
+ calendarHtml += '<td>' + weeks[i] + '</td>'
376
+
377
+ };
378
+
379
+
380
+
381
+
382
+
383
+
384
+
385
+
386
+
387
+ //ここからカレンダー作成
388
+
389
+ //---------------------------------------------------------------------
390
+
391
+ for (let w = 0; w < 6; w++) {
392
+
393
+
394
+
395
+ calendarHtml += '<tr>'
396
+
397
+
398
+
399
+ for (let d = 0; d < 7; d++) { //0~6まで インクリメント 7日で繰り返す
400
+
401
+
402
+
403
+ if (w == 0 && d < startDay) { //wが0かつ5より小さいとき空
404
+
405
+ // 1行目で1日の曜日の前
406
+
407
+
408
+
409
+ let num = lastMonthendDayCount - startDay + d + 1
410
+
411
+
412
+
413
+ calendarHtml += '<td class="is-disabled">' + num + '</td>'
414
+
415
+
416
+
417
+
418
+
419
+ } else if (dayCount > endDayCount) {
420
+
421
+ // 末尾の日数を超えた
422
+
423
+
424
+
425
+
426
+
427
+ let num = dayCount - endDayCount
428
+
429
+ calendarHtml += '<td class="is-disabled">' + num + '</td>'
430
+
431
+ dayCount++
432
+
433
+
434
+
435
+ } else {
436
+
437
+ let year1 = date.getFullYear();
438
+
439
+ const startDate2 = new Date(year, month - 1, 1)
440
+
441
+ const startDay2 = startDate2.getDay() // 月の最初の日の曜日を取得
442
+
443
+ const todays2 = date.getDate();
444
+
445
+ let today_month2 = date.getMonth() + 1;
446
+
447
+ let today_c2 = (6+todays2 + startDay2)
448
+
449
+
450
+
451
+ calendarHtml += `<td id= `+ `"` + year1 + today_month2 + dayCount + `"` + ` ` + `class="calendar_td"` +
452
+
453
+ `daylength=${dayCount}>${dayCount}`+
454
+
455
+ '<br>'
456
+
457
+ + '<input id="' + year1 + todaymonth + dayCount +'day_save"' + 'type = "text" name="" value=""' + ' >' +
458
+
459
+ '<br/> '+
460
+
461
+ '<input id="' + year1 + todaymonth + dayCount +'dayoverTime"' + 'type = "number" name="in" step="0.5" value=""' + ' >' +
462
+
463
+ '<br/> '
464
+
465
+ +' <input id="' + year1 + todaymonth + dayCount +'dayoverTime"'+' onclick="load_click(this.id);" class="btn1" type="button" value="読込" onclick="load();"/>'+
466
+
467
+ ' <input id="' + year1 + todaymonth + dayCount +'day"'+' class="btn2" type="button" value="保存" onclick="reply_click(this.id);save()"/>'+
468
+
469
+ ' <br/><br/>'+
470
+
471
+ ' <button type="button" class="btn btn-primary btn-block" id="' + year1 + todaymonth + dayCount +'dayoverTime"' + 'onclick="reset_click(this.id);">リセット</button>' + '</td>'
472
+
473
+ dayCount++
474
+
475
+ }
476
+
477
+ }
478
+
479
+ calendarHtml += '</tr>'
480
+
481
+ };
482
+
483
+
484
+
485
+ //ここまでカレンダー作成
486
+
487
+ //---------------------------------------------------------------------
488
+
489
+
490
+
491
+ const todays = date.getDate();
492
+
493
+ let today_month = date.getMonth() + 1;
494
+
495
+ let today_c = (5+todays+startDay)
496
+
497
+ const year_index = '<h2>' + year + '/' + (month-1) + '</h2>'
498
+
499
+
500
+
501
+
502
+
503
+  if(year_index == '<h2>' + year + '/' + today_month + '<h2>' ) {
504
+
505
+
506
+
507
+ let cl = document.getElementsByTagName("td");
508
+
509
+ cl[today_c].classList.add('item');
510
+
511
+ };
512
+
513
+ //---------------------------------------------------------------------
514
+
515
+
516
+
517
+
518
+
519
+ calendarHtml += '</tbody>' + '</table>'
520
+
521
+
522
+
523
+ return calendarHtml //ここまでの処理結果を下のshowCalendarに返す
524
+
525
+
526
+
527
+ };
528
+
529
+ //---------------------------------------------------------------------
530
+
531
+
532
+
533
+
534
+
535
+
536
+
537
+
538
+
539
+
540
+
541
+ function moveCalendar(e) {
542
+
543
+ document.querySelector('#calendar').innerHTML = ''
544
+
545
+
546
+
547
+ if (e.target.id === 'prev') {
548
+
549
+ month--
550
+
551
+
552
+
553
+ if (month < 1) {
554
+
555
+ year--
556
+
557
+ month = 12
558
+
559
+ }
560
+
561
+ }
562
+
563
+
564
+
565
+ if (e.target.id === 'next') {
566
+
567
+ month++
568
+
569
+
570
+
571
+ if (month > 12) {
572
+
573
+ year++
574
+
575
+ month = 1
576
+
577
+ }
578
+
579
+ }
580
+
581
+
582
+
583
+ showCalendar(year, month)
584
+
585
+ };
586
+
587
+
588
+
589
+
590
+
591
+
592
+
593
+
594
+
595
+
596
+
597
+
598
+
599
+ document.querySelector('#prev').addEventListener('click', moveCalendar)
600
+
601
+ document.querySelector('#next').addEventListener('click', moveCalendar)
602
+
603
+
604
+
605
+ showCalendar(year, month);
606
+
607
+
608
+
609
+
610
+
611
+
612
+
613
+
614
+
615
+
616
+
617
+
618
+
619
+
620
+
621
+
622
+
623
+ window.onload = function (){
624
+
625
+ let ret = document.getElementById("month_id"+ year1 +month)
626
+
627
+ .getElementsByTagName("td")
628
+
629
+ [today_c2].classList.add("item");
630
+
631
+  };
632
+
633
+
634
+
635
+
636
+
637
+
638
+
639
+
640
+
641
+ // 保存 id 201911day
642
+
643
+ function reply_click(clicked_id)
644
+
645
+ {
646
+
647
+ //alert(clicked_id);
648
+
649
+ let num_day = (clicked_id);
650
+
651
+
652
+
653
+ console.log(num_day);
654
+
655
+
656
+
657
+ save(num_day);
658
+
659
+
660
+
661
+    };
662
+
663
+
664
+
665
+
666
+
667
+
668
+
669
+ // 保存
670
+
671
+ function save(num_day) {
672
+
673
+
674
+
675
+ let mydata = document.getElementById(num_day+'overTime').value;
676
+
677
+ document.getElementById(num_day+'_save').value = mydata
678
+
679
+ console.log(mydata);
680
+
681
+ localStorage.setItem(num_day+'overTime', mydata);
682
+
683
+
684
+
685
+ };
686
+
687
+
688
+
689
+
690
+
691
+
692
+
693
+
694
+
695
+
696
+
697
+   // 読込 id 201911dayoverTime
698
+
699
+ function load_click(clicked_id){
700
+
701
+ //alert(clicked_id);
702
+
703
+ let load_data = (clicked_id);
704
+
705
+ load(load_data);
706
+
707
+    };
708
+
709
+
710
+
711
+
712
+
713
+
714
+
715
+ // 読込
716
+
717
+
718
+
719
+ function load(load_data) {
720
+
721
+ let memo = 'メモ書きは,';
722
+
723
+ let mydata = "";
724
+
725
+
726
+
727
+    
728
+
729
+ if (!localStorage.getItem(load_data)) {
730
+
731
+ mydata = "データがありません";
732
+
733
+ console.log(`mydata= ${mydata}`);
734
+
735
+ } else {
736
+
737
+ mydata = load_data + '=' + localStorage.getItem(load_data);
738
+
739
+ console.log(`mydata= ${mydata}`);
740
+
741
+ var str = mydata;
742
+
743
+ console.log(str);
744
+
745
+
746
+
747
+ }
748
+
749
+   }
750
+
751
+
752
+
753
+
754
+
755
+
756
+
757
+
758
+
759
+
760
+
761
+ //リセット
762
+
763
+
764
+
765
+ function reset_click(clicked_id){
766
+
767
+ console.log(clicked_id);
768
+
769
+
770
+
771
+ if (!localStorage.getItem(clicked_id)) {
772
+
773
+ r_mydata = "データがありません";
774
+
775
+ console.log(r_mydata);
776
+
777
+ } else {
778
+
779
+ r_mydata = clicked_id + '=' + localStorage.getItem(clicked_id);
780
+
781
+ console.log(r_mydata);
782
+
783
+ localStorage.removeItem(clicked_id,r_mydata);
784
+
785
+ console.log(clicked_id);
786
+
787
+ }
788
+
789
+ }
790
+
791
+
792
+
793
+
794
+
795
+
796
+
797
+
798
+
799
+
800
+
801
+
802
+
23
803
  window.onload = function() {
24
804
 
25
805
  const date = new Date(); //今日の日付
@@ -38,44 +818,24 @@
38
818
 
39
819
 
40
820
 
41
-
42
-
43
-
44
-
45
-
46
-
47
821
  for (let len = 0; len < d+1; len++){
48
822
 
49
-
50
-
51
- var overvalue = (localStorage.getItem(year.toString()+month.toString()+len.toString()+'dayoverTime'));
823
+ var overvalue = (localStorage.getItem(year.toString()+month.toString()+len.toString()+'dayoverTime'));
52
-
53
-
54
-
824
+
55
- var set_num = Number(overvalue);
825
+ var set_num = Number(overvalue);
826
+
56
-
827
+ }
57
-
58
-
59
-
60
-
828
+
829
+
830
+
61
- //ここで値を加算してみたが これだと意味ないですが他に思いつきません
831
+ const element_sum = document.getElementById('201911_sum');
62
-
63
-   if (!isNaN(set_num)){
832
+
64
-
65
-   set_num += set_num;
833
+ element_sum.innerHTML = set_num;
66
-
67
-    console.log(set_num)
834
+
68
-
835
+
836
+
69
-    };
837
+ };
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
- }
838
+
78
-
79
- };
839
+
80
840
 
81
841
  ```