質問編集履歴
1
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -5,174 +5,6 @@
|
|
5
5
|
アプリの中にカレンダーを入れたくjavascriptでカレンダーを作ったのですが、javascriptが効いていないためカレンダーが表示させません。
|
6
6
|
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
### 該当のソースコード
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
```javascript
|
16
|
-
|
17
|
-
/html,css,javascriptの順になっています。
|
18
|
-
|
19
|
-
<!-- カレンダー -->
|
20
|
-
|
21
|
-
<h2 class="calendar-title"><span id="js-year"></span>年 <span id="js-month"></span>月</h2>
|
22
|
-
|
23
|
-
<table class="calendar-table">
|
24
|
-
|
25
|
-
<thead>
|
26
|
-
|
27
|
-
<tr><th>日</th><th>月</th><th>火</th><th>水</th><th>木</th><th>金</th><th>土</th></tr>
|
28
|
-
|
29
|
-
</thead>
|
30
|
-
|
31
|
-
<tbody id="js-calendar-body"></tbody>
|
32
|
-
|
33
|
-
</table>
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
<style>
|
38
|
-
|
39
|
-
.calendar-title {
|
40
|
-
|
41
|
-
text-align: center;
|
42
|
-
|
43
|
-
}
|
44
|
-
|
45
|
-
.calendar-table {
|
46
|
-
|
47
|
-
margin: 0 auto;
|
48
|
-
|
49
|
-
.is-today {
|
50
|
-
|
51
|
-
color: red;
|
52
|
-
|
53
|
-
}
|
54
|
-
|
55
|
-
td {
|
56
|
-
|
57
|
-
width: 20px;
|
58
|
-
|
59
|
-
height: 20px;
|
60
|
-
|
61
|
-
line-height: 20px;
|
62
|
-
|
63
|
-
text-align: center;
|
64
|
-
|
65
|
-
}
|
66
|
-
|
67
|
-
}
|
68
|
-
|
69
|
-
</style>
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
<<script>
|
74
|
-
|
75
|
-
var $window = $(window);
|
76
|
-
|
77
|
-
var $year = $('#js-year');
|
78
|
-
|
79
|
-
var $month = $('#js-month');
|
80
|
-
|
81
|
-
var $tbody = $('#js-calendar-body');
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
var today = new Date();
|
86
|
-
|
87
|
-
var currentYear = today.getFullYear(),
|
88
|
-
|
89
|
-
currentMonth = today.getMonth();
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
$window.on('load',function(){
|
94
|
-
|
95
|
-
calendarHeading(currentYear, currentMonth);
|
96
|
-
|
97
|
-
calendarBody(currentYear, currentMonth, today);
|
98
|
-
|
99
|
-
});
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
function calendarBody(year, month, today){
|
104
|
-
|
105
|
-
var todayYMFlag = today.getFullYear() === year && today.getMonth() === month ? true : false; // 本日の年と月が表示されるカレンダーと同じか判定
|
106
|
-
|
107
|
-
var startDate = new Date(year, month, 1); // その月の最初の日の情報
|
108
|
-
|
109
|
-
var endDate = new Date(year, month + 1 , 0); // その月の最後の日の情報
|
110
|
-
|
111
|
-
var startDay = startDate.getDay();// その月の最初の日の曜日を取得
|
112
|
-
|
113
|
-
var endDay = endDate.getDate();// その月の最後の日の曜日を取得
|
114
|
-
|
115
|
-
var textSkip = true; // 日にちを埋める用のフラグ
|
116
|
-
|
117
|
-
var textDate = 1; // 日付(これがカウントアップされます)
|
118
|
-
|
119
|
-
var tableBody =''; // テーブルのHTMLを格納する変数
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
for (var row = 0; row < 6; row++){
|
124
|
-
|
125
|
-
var tr = '<tr>';
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
for (var col = 0; col < 7; col++) {
|
130
|
-
|
131
|
-
if (row === 0 && startDay === col){
|
132
|
-
|
133
|
-
textSkip = false;
|
134
|
-
|
135
|
-
}
|
136
|
-
|
137
|
-
if (textDate > endDay) {
|
138
|
-
|
139
|
-
textSkip = true;
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
var addClass = todayYMFlag && textDate === today.getDate() ? 'is-today' : '';
|
144
|
-
|
145
|
-
var textTd = textSkip ? ' ' : textDate++;
|
146
|
-
|
147
|
-
var td = '<td class="'+addClass+'">'+textTd+'</td>';
|
148
|
-
|
149
|
-
tr += td;
|
150
|
-
|
151
|
-
}
|
152
|
-
|
153
|
-
tr += '</tr>';
|
154
|
-
|
155
|
-
tableBody += tr;
|
156
|
-
|
157
|
-
}
|
158
|
-
|
159
|
-
$tbody.html(tableBody);
|
160
|
-
|
161
|
-
}
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
function calendarHeading(year, month){
|
166
|
-
|
167
|
-
$year.text(year);
|
168
|
-
|
169
|
-
$month.text(month + 1);
|
170
|
-
|
171
|
-
}
|
172
|
-
|
173
|
-
</script>
|
174
|
-
|
175
|
-
```
|
176
8
|
|
177
9
|
|
178
10
|
|