用javascript写一个动态表,第一行居中为“技术网络科考勤表”,第二行居中为年月,年的选择范围为2022年到2099年,月的选择范围为1-12月,根据当年闰年还是平年,可生成2月天数。第三行从左到右根据年月生成当月日期,第四行根据第三行生成当星期几。此表格第一列为人名,分别为渠际时、陈海峰、梁胜玉、袁凯、侯博文、孙逍遥、郭智豪、杨沐松。,中间内容分为三列,分别为“早”、“行政班”、“生产班”、“夜班”。每个里面可选择√输入。最后一列统计出此行输入√天数。
时间: 2024-03-15 14:47:58 浏览: 91
好的,以下是使用 JavaScript 实现的动态表格代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>技术网络科考勤表</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 5px;
text-align: center;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th colspan="7">技术网络科考勤表</th>
</tr>
<tr>
<th colspan="7">
<select id="year"></select> 年
<select id="month"></select> 月
</th>
</tr>
<tr id="dateRow"></tr>
<tr id="dayOfWeekRow"></tr>
<tr>
<th>姓名</th>
<th>早</th>
<th>行政班</th>
<th>生产班</th>
<th>夜班</th>
<th>统计</th>
</tr>
</thead>
<tbody>
<tr>
<td>渠际时</td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td class="total"></td>
</tr>
<tr>
<td>陈海峰</td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td class="total"></td>
</tr>
<tr>
<td>梁胜玉</td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td class="total"></td>
</tr>
<tr>
<td>袁凯</td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td class="total"></td>
</tr>
<tr>
<td>侯博文</td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td class="total"></td>
</tr>
<tr>
<td>孙逍遥</td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td class="total"></td>
</tr>
<tr>
<td>郭智豪</td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td class="total"></td>
</tr>
<tr>
<td>杨沐松</td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td><input type="checkbox" class="day"></td>
<td class="total"></td>
</tr>
</tbody>
</table>
<script>
// 获取年月的下拉框
const yearSelect = document.getElementById("year");
const monthSelect = document.getElementById("month");
// 获取日期行和星期行
const dateRow = document.getElementById("dateRow");
const dayOfWeekRow = document.getElementById("dayOfWeekRow");
// 获取天数输入框和统计列
const days = document.querySelectorAll(".day");
const totalCols = document.querySelectorAll(".total");
// 初始化年份下拉框
for (let year = 2022; year <= 2099; year++) {
const option = document.createElement("option");
option.value = year;
option.text = year;
yearSelect.appendChild(option);
}
// 初始化月份下拉框
for (let month = 1; month <= 12; month++) {
const option = document.createElement("option");
option.value = month;
option.text = month;
monthSelect.appendChild(option);
}
// 更新日期和星期
function updateDateAndDayOfWeek() {
// 获取当前选择的年月
const year = parseInt(yearSelect.value);
const month = parseInt(monthSelect.value);
// 计算当月天数
let daysInMonth;
if (month === 2) {
if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
} else if ([4, 6, 9, 11].includes(month)) {
daysInMonth = 30;
} else {
daysInMonth = 31;
}
// 更新日期行和星期行
dateRow.innerHTML = "";
dayOfWeekRow.innerHTML = "";
for (let day = 1; day <= daysInMonth; day++) {
const dateCell = document.createElement("th");
dateCell.textContent = day;
dateRow.appendChild(dateCell);
const dayOfWeekCell = document.createElement("td");
dayOfWeekCell.textContent = new Date(year, month - 1, day).toLocaleDateString("zh-CN", {weekday: "short"});
dayOfWeekRow.appendChild(dayOfWeekCell);
}
}
// 统计每个人的√天数
function updateTotal() {
totalCols.forEach(totalCol => {
const daysChecked = Array.from(totalCol.parentElement.querySelectorAll(".day")).filter(day => day.checked);
totalCol.textContent = daysChecked.length;
});
}
// 监听年份和月份的变化,更新日期和星期
yearSelect.addEventListener("change", updateDateAndDayOfWeek);
monthSelect.addEventListener("change", updateDateAndDayOfWeek);
// 监听√天数的变化,更新统计
days.forEach(day => {
day.addEventListener("change", updateTotal);
});
// 初始化表格
updateDateAndDayOfWeek();
updateTotal();
</script>
</body>
</html>
```
希望这个代码可以帮助你完成需求,如果有任何问题,请随时提出。
阅读全文