回答編集履歴
2
訂正
test
CHANGED
@@ -262,7 +262,7 @@
|
|
262
262
|
|
263
263
|
],
|
264
264
|
|
265
|
-
isToday = ny === ty && nm === tm ? nd + f: -1;
|
265
|
+
isToday = ny === ty && nm === tm ? nd + f -1: -1;
|
266
266
|
|
267
267
|
|
268
268
|
|
1
自分なりに書いてみた。ちょいオブジェクト指向的な・・・
test
CHANGED
@@ -63,3 +63,327 @@
|
|
63
63
|
}
|
64
64
|
|
65
65
|
```
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
そのうち万年カレンダーでも書いてみようかなと思っていたので書いてみた。
|
70
|
+
|
71
|
+
先月と来月の余計な日付と、今日という存在がロジックを複雑にするね。
|
72
|
+
|
73
|
+
もうちょっと熟考する余地があるね。
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
```html
|
78
|
+
|
79
|
+
<html lang="ja">
|
80
|
+
|
81
|
+
<meta charset="utf-8">
|
82
|
+
|
83
|
+
<title></title>
|
84
|
+
|
85
|
+
<style>
|
86
|
+
|
87
|
+
td {
|
88
|
+
|
89
|
+
text-align: center;
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
tr td:first-Of-type {
|
94
|
+
|
95
|
+
color: red;
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
tr td:nth-Of-type(7) {
|
100
|
+
|
101
|
+
color: blue;
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
td.is-disabled {
|
106
|
+
|
107
|
+
color: silver !important;
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
td.today {
|
112
|
+
|
113
|
+
background: yellow;
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
</style>
|
120
|
+
|
121
|
+
<body>
|
122
|
+
|
123
|
+
<div id="hoge"></div>
|
124
|
+
|
125
|
+
<input type="button" value="prev" id="prev">
|
126
|
+
|
127
|
+
<input type="button" value="next" id="next">
|
128
|
+
|
129
|
+
<hr>
|
130
|
+
|
131
|
+
<div id="fuga"></div>
|
132
|
+
|
133
|
+
<input type="button" value="prev" id="prev2">
|
134
|
+
|
135
|
+
<input type="button" value="next" id="next2">
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
<script>
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
{
|
146
|
+
|
147
|
+
const
|
148
|
+
|
149
|
+
WEEK_JP = ['日', '月', '火', '水', '木', '金', '土'],
|
150
|
+
|
151
|
+
DATE_FN = ['getFullYear', 'getMonth', 'getDate', 'getDay'],
|
152
|
+
|
153
|
+
OPTION = {
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
};
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
function addMonth (dt, n) {
|
162
|
+
|
163
|
+
dt.setMonth (dt.getMonth () + n);
|
164
|
+
|
165
|
+
return dt;
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
function getDTValue (dt = new Date) {
|
172
|
+
|
173
|
+
return DATE_FN.map (f => dt[f]());
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
function removeChild (e) {
|
180
|
+
|
181
|
+
e.remove ();
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
//____________________
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
function init () {
|
192
|
+
|
193
|
+
let
|
194
|
+
|
195
|
+
doc = this.parent.ownerDocument,
|
196
|
+
|
197
|
+
tbl = doc.createElement ('table'),
|
198
|
+
|
199
|
+
thd = tbl.createTHead (),
|
200
|
+
|
201
|
+
tr = thd.insertRow ();
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
tbl.createCaption ();
|
206
|
+
|
207
|
+
WEEK_JP.forEach (w => tr.insertCell ().textContent = w);
|
208
|
+
|
209
|
+
this.table = this.parent.appendChild (tbl);
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
//____________________
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
class Calendar {
|
220
|
+
|
221
|
+
constructor (parent, dateObj = new Date, option = {}) {
|
222
|
+
|
223
|
+
this.parent = parent;
|
224
|
+
|
225
|
+
this.current = dateObj;
|
226
|
+
|
227
|
+
this.option = Object.assign ({}, OPTION, option);
|
228
|
+
|
229
|
+
init.call (this);
|
230
|
+
|
231
|
+
this.show ();
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
show () {
|
238
|
+
|
239
|
+
let
|
240
|
+
|
241
|
+
tbodies = [...this.table.tBodies],
|
242
|
+
|
243
|
+
tbody = this.parent.ownerDocument.createElement ('tbody'),
|
244
|
+
|
245
|
+
[ty, tm, td, tw] = getDTValue (this.current),
|
246
|
+
|
247
|
+
[cy, cm, cd, cw] = getDTValue (new Date (ty, tm + 1, 0)),//今月の月末
|
248
|
+
|
249
|
+
[py, pm, pd, pw] = getDTValue (new Date (ty, tm, 0)),//先月の月末
|
250
|
+
|
251
|
+
[ny, nm, nd] = getDTValue (),
|
252
|
+
|
253
|
+
f = (pw + 1) % 7, //1日の曜日
|
254
|
+
|
255
|
+
map = [
|
256
|
+
|
257
|
+
...Array.from (Array (f), (_, i) => pd - f + i + 1),//先月の日
|
258
|
+
|
259
|
+
...Array.from (Array (cd), (_, i) => i + 1),//今月の日
|
260
|
+
|
261
|
+
...Array.from (Array (6 - cw), (_, i) => i + 1)//翌月の日
|
262
|
+
|
263
|
+
],
|
264
|
+
|
265
|
+
isToday = ny === ty && nm === tm ? nd + f: -1;
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
tbodies.forEach (removeChild);
|
270
|
+
|
271
|
+
this.table.caption.textContent = [cy, cm + 1].join (' / ');
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
for (let cnt = 0, m; map.length; ) {
|
276
|
+
|
277
|
+
m = map.splice (0, 7);
|
278
|
+
|
279
|
+
let tr = tbody.insertRow (-1);
|
280
|
+
|
281
|
+
for (let n of m) {
|
282
|
+
|
283
|
+
let td = tr.insertCell (-1);
|
284
|
+
|
285
|
+
td.textContent = n;
|
286
|
+
|
287
|
+
if (f > cnt || cnt >= (f + cd))
|
288
|
+
|
289
|
+
td.classList.add ('is-disabled');
|
290
|
+
|
291
|
+
else if (cnt == isToday)
|
292
|
+
|
293
|
+
td.classList.add ('today');
|
294
|
+
|
295
|
+
cnt++;
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
this.table.appendChild (tbody);
|
304
|
+
|
305
|
+
return this;
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
next (n) {
|
314
|
+
|
315
|
+
addMonth (this.current, n);
|
316
|
+
|
317
|
+
return this.show ();
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
this.Calendar = Calendar;
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
//_________________________
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
const calendar = new Calendar (hoge);
|
338
|
+
|
339
|
+
calendar.handleEvent = function (event) {
|
340
|
+
|
341
|
+
switch (event.target.id) {
|
342
|
+
|
343
|
+
case 'prev': this.next (-1); break;
|
344
|
+
|
345
|
+
case 'next': this.next (1); break;
|
346
|
+
|
347
|
+
}
|
348
|
+
|
349
|
+
};
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
document.addEventListener ('click', calendar, false);
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
//______________________________
|
358
|
+
|
359
|
+
//2つ目
|
360
|
+
|
361
|
+
const calendar2 = new Calendar (fuga, new Date (2020, 7, 1));
|
362
|
+
|
363
|
+
calendar2.handleEvent = function (event) {
|
364
|
+
|
365
|
+
switch (event.target) {
|
366
|
+
|
367
|
+
case prev2: this.next (-1); break;
|
368
|
+
|
369
|
+
case next2: this.next (1); break;
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
};
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
document.addEventListener ('click', calendar2, false);
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
</script>
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
```
|