所以我只想在我的日历 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/