<!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>
* {
margin: 0px;
padding: 0px;
list-style: none;
}
#calendar {
width: 700px;
background-color: lightgray;
margin: 20px auto;
}
#month_year {
width: 700px;
height: 50px;
line-height: 50px;
text-align: center;
}
ul li {
float: left;
width: 100px;
height: 50px;
text-align: center;
line-height: 50px;
}
</style>
</head>
<body>
<div id="calendar">
<div id="month_year">
<select name="" id="year"></select>
<select name="" id="month"></select>
</div>
<ul id="title">
<li>星期日</li>
<li>星期一</li>
<li>星期二</li>
<li>星期三</li>
<li>星期四</li>
<li>星期五</li>
<li>星期六</li>
</ul>
<ul id="dateul"></ul>
</div>
<script>
var yearSelect = document.getElementById("year");
var monthSelect = document.getElementById("month");
var dateul = document.getElementById("dateul");
//初始化
function init() {
//初始年份
for (var year = 1999; year < 3000; year++) {
createOption(year, year, yearSelect);
}
//初始月份
for (var month = 1; month < 13; month++) {
createOption(month, month - 1, monthSelect);
}
var now = new Date();
showSelect(now.getFullYear(), now.getMonth());
showDates();
yearSelect.onchange = function() {
showDates();
}
monthSelect.onchange = function() {
showDates();
}
}
function createOption(text, value, parent) {
var option = document.createElement("option");
option.innerHTML = text;
option.value = value;
parent.appendChild(option);
}
function showSelect(year, month) {
yearSelect.value = year;
monthSelect.value = month;
}
function showDates() {
dateul.innerHTML = "";
var year = yearSelect.value;
var month = monthSelect.value;
for (var i = 0; i < getDays(year, month); i++) {
createLi("", dateul);
}
for (var j = 1; j < getDatesOfMonth(year, month); j++) {
createLi(j, dateul);
}
}
function getDays(year, month) {
var d = new Date(year, month, 1);
return d.getDay();
}
function createLi(text, parent) {
var li = document.createElement("li");
li.innerHTML = text;
parent.appendChild(li);
}
function getDatesOfMonth(year, month) {
var d = new Date(year, month + 1, 0);
return d.getDate();
}
init();
</script>
</body>
</html>
分享一个自己的小日历
最新推荐文章于 2024-08-19 15:50:15 发布