利用HTML、CSS、JavaScript写一个可以跳转日期的万年历页面
时间: 2024-05-14 10:17:16 浏览: 167
基于html+css+javascrpt前端页面(可在浏览器直接运行)
5星 · 资源好评率100%
好的,下面是一个简单的实现:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>万年历</title>
<style>
body {
font-size: 16px;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
h1 {
text-align: center;
margin-top: 30px;
margin-bottom: 20px;
}
.cal-table {
margin: 0 auto;
border-collapse: collapse;
border-spacing: 0;
width: 80%;
max-width: 800px;
background-color: white;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
.cal-table th,
.cal-table td {
padding: 10px;
text-align: center;
border: 1px solid #ddd;
}
.cal-table th {
font-weight: bold;
background-color: #f5f5f5;
}
.cal-table td {
background-color: #fff;
}
.cal-table td.today {
background-color: #c3e6cb;
font-weight: bold;
}
.cal-table td.selected {
background-color: #007bff;
color: white;
font-weight: bold;
}
.cal-table td:not(.today):not(.selected):hover {
background-color: #f5f5f5;
cursor: pointer;
}
.cal-btn-group {
margin-top: 20px;
text-align: center;
}
.cal-btn {
display: inline-block;
padding: 10px 20px;
margin: 0 10px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
font-size: inherit;
cursor: pointer;
}
.cal-btn:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1>万年历</h1>
<table class="cal-table">
<thead>
<tr>
<th>日</th>
<th>一</th>
<th>二</th>
<th>三</th>
<th>四</th>
<th>五</th>
<th>六</th>
</tr>
</thead>
<tbody id="cal-body"></tbody>
</table>
<div class="cal-btn-group">
<button class="cal-btn" id="prev-year-btn">上一年</button>
<button class="cal-btn" id="prev-month-btn">上一月</button>
<button class="cal-btn" id="next-month-btn">下一月</button>
<button class="cal-btn" id="next-year-btn">下一年</button>
</div>
<script>
// 获取当前日期
var now = new Date();
var currentYear = now.getFullYear();
var currentMonth = now.getMonth() + 1;
var currentDate = now.getDate();
// 生成万年历
function generateCalendar(year, month) {
var calBody = document.getElementById("cal-body");
calBody.innerHTML = "";
var firstDay = new Date(year, month - 1, 1);
var lastDay = new Date(year, month, 0);
var firstDayOfWeek = firstDay.getDay();
var lastDayOfWeek = lastDay.getDay();
var daysInMonth = lastDay.getDate();
var rows = Math.ceil((firstDayOfWeek + daysInMonth) / 7);
for (var i = 0; i < rows; i++) {
var row = document.createElement("tr");
for (var j = 0; j < 7; j++) {
var cell = document.createElement("td");
if (i == 0 && j < firstDayOfWeek || i == rows - 1 && j > lastDayOfWeek) {
cell.textContent = "";
} else {
var day = i * 7 + j + 1 - firstDayOfWeek;
cell.textContent = day;
if (year == currentYear && month == currentMonth && day == currentDate) {
cell.classList.add("today");
}
cell.addEventListener("click", function() {
var selectedCells = document.getElementsByClassName("selected");
for (var i = 0; i < selectedCells.length; i++) {
selectedCells[i].classList.remove("selected");
}
this.classList.add("selected");
});
}
row.appendChild(cell);
}
calBody.appendChild(row);
}
}
// 切换月份
document.getElementById("prev-year-btn").addEventListener("click", function() {
currentYear--;
generateCalendar(currentYear, currentMonth);
});
document.getElementById("prev-month-btn").addEventListener("click", function() {
if (currentMonth == 1) {
currentYear--;
currentMonth = 12;
} else {
currentMonth--;
}
generateCalendar(currentYear, currentMonth);
});
document.getElementById("next-month-btn").addEventListener("click", function() {
if (currentMonth == 12) {
currentYear++;
currentMonth = 1;
} else {
currentMonth++;
}
generateCalendar(currentYear, currentMonth);
});
document.getElementById("next-year-btn").addEventListener("click", function() {
currentYear++;
generateCalendar(currentYear, currentMonth);
});
// 初始化万年历
generateCalendar(currentYear, currentMonth);
</script>
</body>
</html>
```
这个页面使用了 HTML、CSS 和 JavaScript 实现,通过生成表格来显示日历,使用按钮来切换月份和年份。你可以将代码复制到一个 HTML 文件中并在浏览器中打开来运行。
阅读全文