JavaScript实现简易日历效果

JavaScript学习记录

利用JavaScript实现简易日历

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>日历</title>
<style>
#tab .active{
    background:white;color:red;}
#tab{
    width:350px;height:600px;background:#CCCACA;margin:0 auto;border:5px solid #CCCACA}
#tab li{
    width:80px;height:90px;border: 1px solid black;display:inline-table;
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你想了解 JavaScript 实现日历吗? JavaScript 可以通过 DOM 操作实现动态生成日历的功能。以下是一个简单的 JavaScript 日历实现示例: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JavaScript 日历示例</title> <style> table { border-collapse: collapse; width: 100%; } th, td { text-align: center; padding: 8px; border: 1px solid black; } th { background-color: #ddd; } td { height: 80px; } .today { background-color: yellow; } </style> </head> <body> <h1>JavaScript 日历示例</h1> <table> <thead> <tr> <th>日</th> <th>一</th> <th>二</th> <th>三</th> <th>四</th> <th>五</th> <th>六</th> </tr> </thead> <tbody id="calendar-body"> </tbody> </table> <script> const date = new Date(); const year = date.getFullYear(); const month = date.getMonth() + 1; const today = date.getDate(); const daysInMonth = new Date(year, month, 0).getDate(); const firstDay = new Date(year, month - 1, 1).getDay(); const calendarBody = document.getElementById('calendar-body'); let dateCells = ''; let day = 1; for (let i = 0; i < 6; i++) { dateCells += '<tr>'; for (let j = 0; j < 7; j++) { if (i === 0 && j < firstDay) { dateCells += '<td></td>'; } else if (day > daysInMonth) { break; } else { if (day === today) { dateCells += '<td class="today">' + day + '</td>'; } else { dateCells += '<td>' + day + '</td>'; } day++; } } dateCells += '</tr>'; if (day > daysInMonth) { break; } } calendarBody.innerHTML = dateCells; </script> </body> </html> ``` 这个示例会生成当前月份的日历,可以根据需要进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值