-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<!DOCTYPE html> | ||
<html lang="zh-TW"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>月曆顯示</title> | ||
<style> | ||
#calendar table { | ||
width: 100%; | ||
border-collapse: collapse; | ||
} | ||
|
||
#calendar th, #calendar td { | ||
border: 1px solid #ddd; | ||
text-align: center; | ||
padding: 8px; | ||
} | ||
|
||
#calendar th { | ||
background-color: #f2f2f2; | ||
} | ||
|
||
.weekend { | ||
background-color: #cceeff; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>月曆顯示</h1> | ||
<label for="year">年份:</label> | ||
<select id="year"></select> | ||
|
||
<label for="month">月份:</label> | ||
<select id="month"></select> | ||
|
||
<div id="calendar"></div> | ||
|
||
<script> | ||
document.addEventListener('DOMContentLoaded', () => { | ||
const yearSelect = document.getElementById('year'); | ||
const monthSelect = document.getElementById('month'); | ||
const calendarDiv = document.getElementById('calendar'); | ||
|
||
// 填充年份選單 | ||
const currentYear = new Date().getFullYear(); | ||
for (let i = currentYear - 10; i <= currentYear + 10; i++) { | ||
const option = document.createElement('option'); | ||
option.value = i; | ||
option.textContent = i; | ||
yearSelect.appendChild(option); | ||
} | ||
|
||
// 填充月份選單 | ||
const months = [ | ||
"1月", "2月", "3月", "4月", "5月", | ||
"6月", "7月", "8月", "9月", "10月", | ||
"11月", "12月" | ||
]; | ||
months.forEach((month, index) => { | ||
const option = document.createElement('option'); | ||
option.value = index; | ||
option.textContent = month; | ||
monthSelect.appendChild(option); | ||
}); | ||
|
||
// 當年份或月份更改時生成月曆 | ||
yearSelect.addEventListener('change', generateCalendar); | ||
monthSelect.addEventListener('change', generateCalendar); | ||
|
||
function generateCalendar() { | ||
const year = parseInt(yearSelect.value); | ||
const month = parseInt(monthSelect.value); | ||
if (!year || isNaN(month)) return; | ||
|
||
const firstDay = new Date(year, month, 1).getDay(); | ||
const daysInMonth = new Date(year, month + 1, 0).getDate(); | ||
|
||
// 清除先前的月曆 | ||
calendarDiv.innerHTML = ''; | ||
|
||
// 建立月曆表格 | ||
const table = document.createElement('table'); | ||
const headerRow = document.createElement('tr'); | ||
const daysOfWeek = ["日", "一", "二", "三", "四", "五", "六"]; | ||
daysOfWeek.forEach(day => { | ||
const th = document.createElement('th'); | ||
th.textContent = day; | ||
headerRow.appendChild(th); | ||
}); | ||
table.appendChild(headerRow); | ||
|
||
// 填充日期 | ||
let row = document.createElement('tr'); | ||
for (let i = 0; i < firstDay; i++) { | ||
row.appendChild(document.createElement('td')); | ||
} | ||
|
||
for (let day = 1; day <= daysInMonth; day++) { | ||
const cell = document.createElement('td'); | ||
cell.textContent = day; | ||
// 如果是週末,設置淺藍色 | ||
if ((firstDay + day - 1) % 7 === 0 || (firstDay + day - 1) % 7 === 6) { | ||
cell.classList.add('weekend'); | ||
} | ||
row.appendChild(cell); | ||
if ((firstDay + day) % 7 === 0) { | ||
table.appendChild(row); | ||
row = document.createElement('tr'); | ||
} | ||
} | ||
|
||
table.appendChild(row); | ||
calendarDiv.appendChild(table); | ||
} | ||
|
||
// 預設為當前月份 | ||
yearSelect.value = currentYear; | ||
monthSelect.value = new Date().getMonth(); | ||
generateCalendar(); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters