HTML简单电子日历
以下是一个简单的HTML电子日历示例,它可以显示当前日期以及前一个月和后一个月的日期,并且可以通过点击日期来选择日期。
<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
}
.calendar {
max-width: 250px;
margin: 50px auto;
padding: 10px;
background-color: #f2f2f2;
border: 1px solid #ddd;
border-radius: 5px;
box-shadow: 2px 2px 5px rgba(0,0,0,.3);
}
.calendar-header {
text-align: center;
padding: 10px 0;
background-color: #4CAF50;
color: white;
}
.calendar-header h1 {
font-size: 18px;
}
.calendar-header h2 {
font-size: 12px;
}
.calendar-days {
padding: 10px 0;
background-color: #f2f2f2;
display: flex;
align-items: center;
}
.calendar-days span {
display: inline-block;
width: 30px;
height: 30px;
text-align: center;
line-height: 30px;
margin: 0 5px;
}
.calendar-days span:hover {
background-color: #ddd;
cursor: pointer;
}
.calendar-month {
text-align: center;
padding: 10px 0;
}
.calendar-month h1 {
font-size: 18px;
}
.calendar-days-of-week {
display: flex;
text-transform: uppercase;
}
.calendar-days-of-week span {
flex: 1;
text-align: center;
}
.day-number {
display: block;
width: 30px;
height: 30px;
line-height: 30px;
text-align: center;
border-radius: 50%;
margin: 0 auto;
}
.day-number:hover {
background-color: #ddd;
cursor: pointer;
}
.selected {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<div class="calendar">
<div class="calendar-header">
<h1 id="month-year"></h1>
<h2 id="weekday"></h2>
</div>
<div class="calendar-month">
<h1 id="month"></h1>
</div>
<div class="calendar-days-of-week">
<span>Sun</span>
<span>Mon</span>
<span>Tue</span>
<span>Wed</span>
<span>Thu</span>
<span>Fri</span>
<span>Sat</span>
</div>
<div class="calendar-days" id="days"></div>
</div>
<script>
function renderCalendar() {
const now = new Date();
const daysInMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0).getDate();
const daysOfWeek = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
let
评论已关闭