在页面上生成当前月份的日历,要求1:可以通过点击“上个月”和“下个月”按钮来切换月份,并且突出当前日期。要求2:连接MySQL数据库,获取每个日期的事件列表,并将它们显示在该日期的单元格上。要求3:当鼠标指向该日期时,可以弹出一个窗口,显示该日期的事件详细信息。请生成前端和后端代码。
时间: 2024-02-16 22:02:42 浏览: 64
好的,以下是前端和后端代码的示例:
前端代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>日历</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 5px;
text-align: center;
}
th {
background-color: #EEE;
}
td.today {
background-color: #F00;
color: #FFF;
}
</style>
</head>
<body>
<h1>日历</h1>
<button id="prevMonthBtn">上个月</button>
<button id="nextMonthBtn">下个月</button>
<table>
<thead>
<tr>
<th>周日</th>
<th>周一</th>
<th>周二</th>
<th>周三</th>
<th>周四</th>
<th>周五</th>
<th>周六</th>
</tr>
</thead>
<tbody id="calendarBody"></tbody>
</table>
<div id="eventDetails"></div>
<script>
// 获取当前日期
var currentDate = new Date();
var currentMonth = currentDate.getMonth();
var currentYear = currentDate.getFullYear();
// 显示当前月份的日历
displayCalendar(currentMonth, currentYear);
// 上个月按钮点击事件
document.getElementById("prevMonthBtn").addEventListener("click", function() {
currentMonth--;
if (currentMonth < 0) {
currentMonth = 11;
currentYear--;
}
displayCalendar(currentMonth, currentYear);
});
// 下个月按钮点击事件
document.getElementById("nextMonthBtn").addEventListener("click", function() {
currentMonth++;
if (currentMonth > 11) {
currentMonth = 0;
currentYear++;
}
displayCalendar(currentMonth, currentYear);
});
// 显示日历
function displayCalendar(month, year) {
var firstDay = new Date(year, month, 1);
var lastDay = new Date(year, month + 1, 0);
var daysInMonth = lastDay.getDate();
var firstDayOfWeek = firstDay.getDay();
var lastDayOfWeek = lastDay.getDay();
var calendarBody = document.getElementById("calendarBody");
calendarBody.innerHTML = "";
// 添加前面的空单元格
var row = calendarBody.insertRow();
for (var i = 0; i < firstDayOfWeek; i++) {
row.insertCell();
}
// 添加日期单元格
var currentDate = 1;
for (var i = firstDayOfWeek; i <= 6; i++) {
var cell = row.insertCell();
cell.innerHTML = currentDate;
cell.setAttribute("data-date", year + "-" + (month + 1) + "-" + currentDate);
currentDate++;
}
for (var i = 1; i <= daysInMonth - currentDate + 1; i++) {
if (currentDate === 1) {
row = calendarBody.insertRow();
}
var cell = row.insertCell();
cell.innerHTML = currentDate;
cell.setAttribute("data-date", year + "-" + (month + 1) + "-" + currentDate);
if (currentDate === new Date().getDate() && month === new Date().getMonth() && year === new Date().getFullYear()) {
cell.classList.add("today");
}
currentDate++;
}
// 添加后面的空单元格
for (var i = lastDayOfWeek + 1; i <= 6; i++) {
row.insertCell();
}
// 获取事件列表并添加到单元格上
var cells = calendarBody.getElementsByTagName("td");
for (var i = 0; i < cells.length; i++) {
var cell = cells[i];
var date = cell.getAttribute("data-date");
getEvents(date, function(events) {
if (events.length > 0) {
var ul = document.createElement("ul");
for (var j = 0; j < events.length; j++) {
var li = document.createElement("li");
li.innerHTML = events[j].title;
ul.appendChild(li);
}
cell.appendChild(ul);
}
});
cell.addEventListener("mouseover", function() {
var date = this.getAttribute("data-date");
getEvents(date, function(events) {
var eventDetails = document.getElementById("eventDetails");
eventDetails.innerHTML = "";
if (events.length > 0) {
var ul = document.createElement("ul");
for (var j = 0; j < events.length; j++) {
var li = document.createElement("li");
li.innerHTML = events[j].title + " - " + events[j].description;
ul.appendChild(li);
}
eventDetails.appendChild(ul);
eventDetails.style.display = "block";
}
});
});
cell.addEventListener("mouseout", function() {
var eventDetails = document.getElementById("eventDetails");
eventDetails.style.display = "none";
});
}
}
// 获取事件列表
function getEvents(date, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var events = JSON.parse(xhr.responseText);
callback(events);
}
};
xhr.open("GET", "get_events.php?date=" + encodeURIComponent(date), true);
xhr.send();
}
</script>
</body>
</html>
```
后端代码(使用PHP):
```php
<?php
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败:" . $conn->connect_error);
}
// 获取日期
if (isset($_GET["date"])) {
$date = $_GET["date"];
} else {
$date = date("Y-m-d");
}
// 查询事件
$sql = "SELECT title, description FROM events WHERE date='$date'";
$result = $conn->query($sql);
$events = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$events[] = $row;
}
}
// 返回事件列表
header("Content-Type: application/json");
echo json_encode($events);
$conn->close();
?>
```
需要注意的是,这只是一个示例代码,需要根据实际情况进行修改和调整。同时,需要在本地或服务器上安装和配置MySQL数据库和PHP环境。
阅读全文