当前位置:首页 > JavaScript

js实现台历

2026-02-01 05:42:13JavaScript

实现台历的 JavaScript 方法

使用 JavaScript 实现台历可以通过动态生成日历表格、处理日期逻辑以及添加交互功能来完成。以下是具体实现步骤:

基础 HTML 结构

创建一个简单的 HTML 结构用于承载日历:

<div id="calendar">
  <div class="calendar-header">
    <button id="prev-month">上一月</button>
    <h2 id="current-month"></h2>
    <button id="next-month">下一月</button>
  </div>
  <table id="calendar-body">
    <thead>
      <tr>
        <th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th>
      </tr>
    </thead>
    <tbody></tbody>
  </table>
</div>

JavaScript 核心逻辑

通过 JavaScript 动态生成日历内容并处理月份切换:

document.addEventListener('DOMContentLoaded', function() {
  let currentDate = new Date();
  const monthDisplay = document.getElementById('current-month');
  const calendarBody = document.querySelector('#calendar-body tbody');

  function renderCalendar(date) {
    const year = date.getFullYear();
    const month = date.getMonth();
    monthDisplay.textContent = `${year}年${month + 1}月`;

    const firstDay = new Date(year, month, 1);
    const lastDay = new Date(year, month + 1, 0);
    const startingDay = firstDay.getDay();

    calendarBody.innerHTML = '';
    let dateNum = 1;

    for (let i = 0; i < 6; i++) {
      const row = document.createElement('tr');
      for (let j = 0; j < 7; j++) {
        const cell = document.createElement('td');
        if (i === 0 && j < startingDay) {
          cell.textContent = '';
        } else if (dateNum > lastDay.getDate()) {
          break;
        } else {
          cell.textContent = dateNum;
          dateNum++;
        }
        row.appendChild(cell);
      }
      calendarBody.appendChild(row);
    }
  }

  document.getElementById('prev-month').addEventListener('click', function() {
    currentDate.setMonth(currentDate.getMonth() - 1);
    renderCalendar(currentDate);
  });

  document.getElementById('next-month').addEventListener('click', function() {
    currentDate.setMonth(currentDate.getMonth() + 1);
    renderCalendar(currentDate);
  });

  renderCalendar(currentDate);
});

样式增强

添加基本 CSS 样式提升视觉效果:

#calendar {
  width: 300px;
  font-family: Arial, sans-serif;
}
.calendar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
#calendar-body {
  width: 100%;
  border-collapse: collapse;
}
#calendar-body th, #calendar-body td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: center;
}
#calendar-body th {
  background-color: #f2f2f2;
}

高级功能扩展

添加日期点击事件

calendarBody.addEventListener('click', function(e) {
  if (e.target.tagName === 'TD' && e.target.textContent) {
    alert(`选中日期: ${currentDate.getFullYear()}年${currentDate.getMonth()+1}月${e.target.textContent}日`);
  }
});

标记当前日期renderCalendar 函数中添加:

const today = new Date();
if (year === today.getFullYear() && month === today.getMonth() && dateNum-1 === today.getDate()) {
  cell.classList.add('today');
}

配套 CSS:

.today {
  background-color: #ffeb3b;
  font-weight: bold;
}

响应式设计

通过媒体查询适配移动设备:

@media (max-width: 600px) {
  #calendar {
    width: 100%;
  }
  #calendar-body th, #calendar-body td {
    padding: 4px;
    font-size: 14px;
  }
}

完整实现方案

将上述代码片段组合后,即可实现一个功能完整的 JavaScript 台历,包含月份切换、日期显示、当前日期标记和点击交互等功能。可根据需要进一步扩展添加节假日标记、日程提醒等高级功能。

js实现台历

标签: 台历js
分享给朋友:

相关文章

js 实现vue

js 实现vue

实现 Vue 的核心功能 在 JavaScript 中实现 Vue 的核心功能需要模拟数据绑定、虚拟 DOM 和响应式系统。以下是一个简化版的实现思路: 响应式系统 通过 Object.define…

js实现轮播

js实现轮播

实现基础轮播效果 使用HTML结构创建轮播容器和图片元素: <div class="carousel"> <div class="carousel-inner">…

js实现分页

js实现分页

分页的基本实现思路 在JavaScript中实现分页功能通常需要结合前端和后端逻辑。前端负责渲染分页控件和处理用户交互,后端负责提供分页数据。 前端分页实现 纯前端分页适用于数据量较小的情况,可以…

js实现倒计时

js实现倒计时

使用 setInterval 实现倒计时 通过 setInterval 定时器每秒更新剩余时间,适用于简单倒计时场景。 function countdown(seconds, callbac…

js 进度条的实现

js 进度条的实现

使用 HTML 和 CSS 创建基础进度条 HTML 结构可以简单使用一个 div 元素作为容器,内部嵌套另一个 div 表示进度: <div class="progress-contain…

jquery.js

jquery.js

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互等操作。以下是关于 jQuery.js 的核心信息和使用方法: 获取…