javascript - 以前选择的日期不会在日历中取消选择

标签 javascript jquery html css

所以我只想在我的日历 UI 上选择一个日期,但是当我按下一个新日期时,旧日期仍然亮着,所以我可以按下我日历上的所有日期,它们都会亮起上。

for(var dayCounter = 1; dayCounter <= currMonthDays; dayCounter++){

    tempListItem = document.createElement("li");
    tempListItem.innerHTML = dayCounter;
    $(tempListItem).addClass("day");
    //add a hidden element to the day that we can access when we click on it
    var temp = months[currMonth] +"/" + dayCounter +"/" + currFullYear;
    $(tempListItem).append("<div class = 'hidden'>" + temp + "</div>");

    if(dayCounter == date.getDate() && currMonth == date.getMonth()){
        tempListItem.setAttribute("id", "current-day");
    }

    ulDays.appendChild(tempListItem);
}
$(document).on("click", ".day", function()
{
    var date = $(this).children(".hidden").text();
    console.log(date);
    $(document.getElementById("current-day")).removeAttr("#current-day");
    $(this).attr("id", "current-day");
});

删除当前类后,元素不应该丢失其 CSS 吗?

最佳答案

当您尝试从 current-day 中删除 id 时,您似乎有一个小 big。 removeAttr 需要属性的名称。在这种情况下,该属性将是 id,所以试试这个:

$(document.getElementById("current-day")).removeAttr("id");

关于javascript - 以前选择的日期不会在日历中取消选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47440367/

相关文章:

javascript - 带有自定义分隔符的 Number.toLocaleString()

javascript - 如何使用jquery获取输入是否有焦点

javascript - 当另一个 PHP 脚本运行时,AJAX 不会调用它的 PHP 脚本

jQuery - 复选框启用/禁用

jquery - 确定其父级中的子级索引

javascript - 如何在 React 中动态更改 CSS 属性的值?

jquery - 如何将按钮放在溢出隐藏 div 的末尾?

javascript - 如何通过将鼠标悬停在 Javascript 中的三个按钮之一上来显示和更新隐藏的 div 元素?

PHP 更改导航栏中的颜色

html - 如何以某种方式使不同尺寸的盒子彼此相邻