2015-2030年的日历,要突出当前日期,并可以通过点击←→按钮更改月份。通过查询数据库,可以按日期把数据表ot_apply中所有employee_id的event内容都以文本的形式输出到对应日期的单元格里。当鼠标指向该文本时会弹出窗口显示该event的详情。请生成完整代码
时间: 2024-02-11 11:06:00 浏览: 94
好的,根据您的要求,我给您生成一个HTML、CSS和JavaScript的代码,如下所示:
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Calendar</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>
<h1>Calendar</h1>
<button id="previousMonth">❮</button>
<button id="nextMonth">❯</button>
<select id="year"></select>
<select id="month"></select>
</header>
<table>
<thead>
<tr>
<th>Sun</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
</tr>
</thead>
<tbody id="calendarBody"></tbody>
</table>
<div id="eventDetails"></div>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```css
body {
font-family: Arial, sans-serif;
}
header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px;
background-color: #f2f2f2;
}
table {
margin: 20px auto;
border-collapse: collapse;
}
th, td {
padding: 10px;
text-align: center;
}
th {
border-bottom: 1px solid #ddd;
}
td {
position: relative;
cursor: pointer;
}
td:hover {
background-color: #f2f2f2;
}
td.active {
background-color: #4CAF50;
color: #fff;
}
#eventDetails {
display: none;
position: absolute;
top: 0;
left: 0;
padding: 10px;
background-color: #fff;
box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.3);
z-index: 9999;
}
```
JavaScript代码:
```javascript
// 获取当前日期
var currentDate = new Date();
// 获取年份和月份的下拉框
var yearDropdown = document.getElementById("year");
var monthDropdown = document.getElementById("month");
// 生成年份的选项
for (var i = 2015; i <= 2030; i++) {
var option = document.createElement("option");
option.value = i;
option.text = i;
yearDropdown.appendChild(option);
}
// 生成月份的选项
for (var i = 1; i <= 12; i++) {
var option = document.createElement("option");
option.value = i;
option.text = i;
monthDropdown.appendChild(option);
}
// 设置年份和月份的选中状态
yearDropdown.value = currentDate.getFullYear();
monthDropdown.value = currentDate.getMonth() + 1;
// 获取日历表格和事件详情弹窗
var calendarBody = document.getElementById("calendarBody");
var eventDetails = document.getElementById("eventDetails");
// 生成日历
generateCalendar(currentDate.getFullYear(), currentDate.getMonth() + 1);
// 为年份和月份的下拉框添加事件监听器
yearDropdown.addEventListener("change", function() {
generateCalendar(yearDropdown.value, monthDropdown.value);
});
monthDropdown.addEventListener("change", function() {
generateCalendar(yearDropdown.value, monthDropdown.value);
});
// 为上一个月和下一个月按钮添加事件监听器
document.getElementById("previousMonth").addEventListener("click", function() {
var currentYear = parseInt(yearDropdown.value);
var currentMonth = parseInt(monthDropdown.value);
if (currentMonth == 1) {
yearDropdown.value = currentYear - 1;
monthDropdown.value = 12;
} else {
monthDropdown.value = currentMonth - 1;
}
generateCalendar(yearDropdown.value, monthDropdown.value);
});
document.getElementById("nextMonth").addEventListener("click", function() {
var currentYear = parseInt(yearDropdown.value);
var currentMonth = parseInt(monthDropdown.value);
if (currentMonth == 12) {
yearDropdown.value = currentYear + 1;
monthDropdown.value = 1;
} else {
monthDropdown.value = currentMonth + 1;
}
generateCalendar(yearDropdown.value, monthDropdown.value);
});
// 生成日历的函数
function generateCalendar(year, month) {
// 清空日历表格和事件详情弹窗
calendarBody.innerHTML = "";
eventDetails.innerHTML = "";
// 获取当前月份的第一天和最后一天
var firstDay = new Date(year, month - 1, 1);
var lastDay = new Date(year, month, 0);
// 获取当前月份的天数和第一天是星期几
var daysInMonth = lastDay.getDate();
var firstDayOfWeek = firstDay.getDay();
// 生成日历表格的表头和表体
var calendarHTML = "<tr>";
for (var i = 0; i < firstDayOfWeek; i++) {
calendarHTML += "<td></td>";
}
for (var i = 1; i <= daysInMonth; i++) {
if ((firstDayOfWeek + i - 1) % 7 == 0) {
calendarHTML += "</tr><tr>";
}
calendarHTML += "<td data-date='" + year + "-" + month + "-" + i + "'>" + i + "</td>";
}
calendarHTML += "</tr>";
calendarBody.innerHTML = calendarHTML;
// 获取所有日期单元格
var dateCells = calendarBody.getElementsByTagName("td");
// 为日期单元格添加事件监听器
for (var i = 0; i < dateCells.length; i++) {
dateCells[i].addEventListener("click", function() {
// 切换选中状态
var selectedCell = calendarBody.getElementsByClassName("active")[0];
if (selectedCell) {
selectedCell.classList.remove("active");
}
this.classList.add("active");
// 获取事件详情
var date = this.getAttribute("data-date");
var events = getEventsForDate(date);
// 显示事件详情弹窗
if (events.length > 0) {
var eventHTML = "";
for (var i = 0; i < events.length; i++) {
eventHTML += "<p>" + events[i] + "</p>";
}
eventDetails.innerHTML = eventHTML;
eventDetails.style.display = "block";
// 设置事件详情弹窗的位置
var cellRect = this.getBoundingClientRect();
eventDetails.style.top = cellRect.top + window.scrollY + "px";
eventDetails.style.left = cellRect.right + window.scrollX + "px";
} else {
eventDetails.style.display = "none";
}
});
}
}
// 获取指定日期的所有事件
function getEventsForDate(date) {
// 这里需要根据您的具体数据库和后台语言来编写代码
// 这里只是一个示例,返回一个随机生成的事件列表
var events = [];
var rand = Math.floor(Math.random() * 5);
for (var i = 0; i < rand; i++) {
events.push("Event " + (i + 1));
}
return events;
}
```
请注意,这里的代码只是一个简单的示例,需要根据您的具体要求和环境进行修改和完善。同时,这里的代码也没有与数据库进行实际交互,需要您根据您的具体情况来编写相关的代码。
阅读全文