You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现日历日期点击变色切换及选中天数统计功能?

解决响应式日历的日期切换与计数器问题

问题分析

现有代码存在几个关键问题:

  • 类名拼写不一致:JS中判断的是vacation,但CSS和添加的类是vacacion,导致取消选中逻辑无法触发。
  • 事件绑定逻辑错误:初始所有.day元素都包含day类,只会绑定第一个点击事件,且移除day类后无法再次触发状态切换。
  • 缺少计数器更新逻辑:没有实现选中天数的统计与更新。

修复后的代码实现

HTML 调整

修正计数器文本使其与功能对应(可选):

<div class="contador"><h6>Días de vacaciones:<span id="contador">0</span></h6></div>

CSS 优化

统一类名,删除重复定义:

* {
    margin: 0;
    padding: 0;
    font-family: Arial, Helvetica, sans-serif;
    color: rgb(65, 65, 65);
}

h1, td, th {
    text-align: center;
}

h1 {
    padding: 30px;
    background-color: rgb(140, 161, 218);
    color: white;
}

div#colors {
    margin: 15px 20px;
    display: grid;
    grid-template-columns: 85px 100px 100px auto;
}

div#colors div.config {
    width: 20px;
    height: 12px;
    float: left;
    margin-right: 3px;
    border-radius: 4px;
}

div#colors div.contador {
    width: 110px;
    justify-items: right;
    font-weight: 900;
}

div#colors div.contador h6 {
    font-weight: 600;
}

.festivos {
    background-color: #7d6da1;
    color: white;
}

.vacacion {
    background-color: #ad626e;
    color: white;
}

.ausencias {
    background-color: #a77f13;
    color: white;
}

div#colors h6 {
    font-size: 0.8em;
    font-weight: 500;
}

div#calendar {
    margin: 15px auto;
    display: grid;
    grid-template-columns: auto auto auto;
    column-gap: 30px;
    row-gap: 30px;
    justify-items: center;
    justify-content: center;
}

table {
    justify-self: center;
    align-self: start;
}

caption {
    background-color: rgb(140, 161, 218);
    padding: 5px;
    color: white;
    font-weight: 900;
}

.finde {
    background-color: rgb(230, 230, 230);
}

th {
    padding: 0 7px 0 7px;

}

td {
    padding: 3px 0 3px 0; 
}

.day {
    background-color: transparent;
}

@media only screen and (min-width: 1200px) {
    div#calendar {
        grid-template-columns: auto auto auto auto;
    }
}

@media only screen and (max-width: 850px) {
    div#calendar {
        grid-template-columns: auto auto;
    }
}

@media only screen and (max-width: 600px) {
    div#calendar {
        grid-template-columns: auto;
    }
}

JavaScript 完整修复

使用classList.toggle()简化状态切换,同时实现计数器逻辑:

// 获取所有可点击的日期元素
const days = document.querySelectorAll('.day');
// 获取计数器元素
const counter = document.getElementById('contador');

// 更新计数器的函数
function updateCounter() {
    // 统计所有带有 vacacion 类的日期数量
    const selectedCount = document.querySelectorAll('.vacacion').length;
    counter.textContent = selectedCount;
}

// 为每个日期绑定点击事件
days.forEach(day => {
    day.addEventListener('click', () => {
        // 自动切换选中状态
        day.classList.toggle('vacacion');
        // 更新计数器
        updateCounter();
    });
});

// 页面加载时初始化计数器
updateCounter();

关键改进点说明

  1. 类名统一:确保JS和CSS中的类名完全一致,避免拼写错误导致样式失效。
  2. toggle方法简化逻辑:classList.toggle()自动判断元素是否包含指定类,无需手动编写添加/移除的分支逻辑。
  3. 计数器实时更新:每次点击后重新统计选中天数,确保计数器与实际选中状态同步。
  4. 初始化计数器:页面加载时执行一次计数器更新,保证初始显示正确。

内容的提问来源于stack exchange,提问作者amiindless

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 03:54:24