动态万年历

image-20240128201809051

  • 通过魔法字符串给整个table添加组件
  • 通过函数获取当前日期,保存到数组中
  • 遍历数组,添加日期
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
margin: 0;
}

#box {
width: 600px;
margin: 50px auto;
border: 1px solid blue;
}

#box header {
height: 50px;
line-height: 50px;
text-align: center;
color: #fff;
background-color: #17a;
padding: 0 10px;
}

#box header span {
cursor: pointer;
}

#box header .fl {
float: left;
}

#box header .fr {
float: right;
}

#box header div {
font-size: 30px;
}

#box table {
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
}

#box table th,
#box table td {
border-left: 1px solid #ccc;
border-top: 1px solid #ccc;
text-align: center;
height: 30px;
}
</style>
</head>

<body>
<div id="box">
<!-- <header>
<span class="fl">上个月</span>
<span class="fr">下个月</span>
<div>2022年10月</div>
</header>
<table width="100%" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th class="red">周日</th>
<th>周一</th>
<th>周二</th>
<th>周三</th>
<th>周四</th>
<th>周五</th>
<th>周六</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table> -->
</div>
</body>
<script src="../jquery.min.js"></script>
<script>
$(function () {
var defaultDate = new Date(2024, 0);
var box = $("#box");
var yearMonthTitleDiv = null;
var prevMonthSpan = null;
var nextMonthSpan = null;
var tBody = null;
var allTd = [];


//创建
create();

//显示当前对应的日期
showDate();

function create() {
var header = $(`
<header>
<span class="fl">上个月</span>
<span class="fr">下个月</span>
<div>2022年10月</div>
</header>
`);
box.append(header);
yearMonthTitleDiv = header.find('div');
prevMonthSpan = header.find('.fl');
nextMonthSpan = header.find('.fr');

var table = $(`
<table width="100%" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th class="red">周日</th>
<th>周一</th>
<th>周二</th>
<th>周三</th>
<th>周四</th>
<th>周五</th>
<th>周六</th>
</tr>
</thead>
<tbody>
</table>
`);
box.append(table)
tBody = table.find('tbody')
for (var i = 0; i < 6; i++) {
var tr = $("<tr></tr>");
for (var k = 0; k < 7; k++) {
var td = $("<td></td>");
tr.append(td);
allTd.push(td);
}
tBody.append(tr)
}



}

function showDate() {
var year = defaultDate.getFullYear();
var month = defaultDate.getMonth() + 1;
// console.log(month);

yearMonthTitleDiv.text(`${year}${month}月`);

var week = new Date(year, month - 1, 1).getDay();//一号是星期几
var days = new Date(year, month, 0).getDate();//当前月的前一个月的最后一天是几号

var n = 1;

for (var i = 0; i < allTd.length; i++) {
allTd[i].empty();
if (i >= week && n <= days) {
allTd[i].text(n);
n++;
}
}
if (allTd[28].text() === '') {
allTd[28].parent().hide();
allTd[35].parent().hide();
} else if (allTd[35].text() === '') {
allTd[35].parent().hide();
}
else {
allTd[35].parent().show();
allTd[28].parent().show();
}


}


$('#box span').click(function () {
var year = defaultDate.getFullYear();
var month = defaultDate.getMonth();
if ($(this).index() == 0) {
month--;
} else {
month++;
}
defaultDate = new Date(year, month);
showDate();
})


})

</script>

</html>