到达日期后添加类的jQuery脚本
我有一个包含多个项目的列表。每个项目都有一个日期。我现在正在寻找一个jQuery脚本,如果当前日期等于或大于元素的日期,它将向元素添加一个类 例如:到达日期后添加类的jQuery脚本,jquery,class,date,Jquery,Class,Date,我有一个包含多个项目的列表。每个项目都有一个日期。我现在正在寻找一个jQuery脚本,如果当前日期等于或大于元素的日期,它将向元素添加一个类 例如: <ul> <li class="entry" data-date="2018-09-25"> Some content here </li> <li class="entry" data-date="2018-09-27"> Some conte
<ul>
<li class="entry" data-date="2018-09-25">
Some content here
</li>
<li class="entry" data-date="2018-09-27">
Some content here
</li>
<li class="entry" data-date="2018-10-05">
Some content here
</li>
</ul>
非常感谢 循环遍历元素,并使用if语句比较日期,然后将类添加到符合您要求的日期中
let todayDate = new Date();
$("li").each(function(){
let liDate = new Date($(this).attr("data-date"));
if(liDate < todayDate){
$(this).addClass("additional-class");
}
});
let todayDate=new Date();
$(“li”)。每个(函数(){
让liDate=新日期($(this).attr(“数据日期”);
如果(liDate<今日日期){
$(本).addClass(“附加类”);
}
});
这可能行得通。您为解决此问题做了哪些努力?我已添加了上面尝试过的代码。
$(function() {
var date = new Date(),
currentDate = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
$(".entry").each(function() {
var specifiedDate = $(this).data('date');
if (specifiedDate >= currentDate) {
$(this).addClass('past');
}
});
});
let todayDate = new Date();
$("li").each(function(){
let liDate = new Date($(this).attr("data-date"));
if(liDate < todayDate){
$(this).addClass("additional-class");
}
});