用JavaScript实现日历表
时间: 2024-05-02 08:19:25 浏览: 7
Sure, I can help you with that! Here's an example of how to create a simple calendar using JavaScript:
```
// Get the current date
var today = new Date();
// Get the year and month
var year = today.getFullYear();
var month = today.getMonth();
// Create a table element for the calendar
var table = document.createElement('table');
// Create a header row for the calendar
var headerRow = document.createElement('tr');
// Create the cells for the header row
for (var i = 0; i < 7; i++) {
var cell = document.createElement('th');
cell.textContent = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][i];
headerRow.appendChild(cell);
}
// Add the header row to the table
table.appendChild(headerRow);
// Create the cells for the calendar
for (var i = 0; i < 6; i++) {
var row = document.createElement('tr');
for (var j = 0; j < 7; j++) {
var cell = document.createElement('td');
// Calculate the date for this cell
var date = new Date(year, month, (i * 7) + j - today.getDay() + 1);
// Set the cell content to the date
cell.textContent = date.getDate();
// Add the cell to the row
row.appendChild(cell);
}
// Add the row to the table
table.appendChild(row);
}
// Add the table to the page
document.body.appendChild(table);
```
This code creates a simple table-based calendar that displays the current month. You can modify it to display other months or add more functionality as needed. Let me know if you have any questions!