我试图运行一个if语句使用jQuery。我相信我遵循了正确的结构,但代码没有运行。我的jQuery如果其他语句不工作
我相信它与语句的性质有关,在语句中,无论我写的if else语句如何都会覆盖另一个语句。
最后其他的if语句有效,但中间的语句不起作用?
见下面的代码小提琴
<body>
<div class="thumb-holder">
<div class="post-title" data-date="Tuesday,14 December 2013" data-time="18:00">
<img class="featured-thumb" src="http://web-vassets.ea.com/Assets/Richmedia/Image/FeaturedGame/crysis3_298x168.jpg?cb=1334583481" alt="tag test" style="width:250px;height:156px" />
</div>
</div>
<div class="thumb-holder">
<div class="post-title" data-date="Tuesday,14 December 2012" data-time="18:00">
<img class="featured-thumb" src="http://web-vassets.ea.com/Assets/Richmedia/Image/FeaturedGame/crysis3_298x168.jpg?cb=1334583481" alt="tag test" style="width:250px;height:156px" />
</div>
</div>
<div calss="thumb-holder">
<div class="post-title" data-date="Tuesday,14 December 2012" data-time="18:00">
<img class="featured-thumb" src="http://web-vassets.ea.com/Assets/Richmedia/Image/FeaturedGame/crysis3_298x168.jpg?cb=1334583481" alt="tag test" style="width:250px;height:156px" />
</div>
</div>
<div class="thumb-holder">
<div class="post-title" data-date="Thursday,27 February 2014" data-time="18:00">
<img class="featured-thumb" src="http://web-vassets.ea.com/Assets/Richmedia/Image/FeaturedGame/crysis3_298x168.jpg?cb=1334583481" alt="tag test" style="width:250px;height:156px" />
</div>
</div>
<div class="thumb-holder">
<div class="post-title" data-date="Thursday,25 December 2014" data-time="18:00">
<img class="featured-thumb" src="http://web-vassets.ea.com/Assets/Richmedia/Image/FeaturedGame/crysis3_298x168.jpg?cb=1334583481" alt="tag test" style="width:250px;height:156px" />
</div>
</div>
$('.post-title').each(function() {
//1 day old
if (new Date($(this).data('date')).getTime() < new Date().getTime()) {
$(this).css({
opacity: 0.5,
border: '2px solid blue'
});
}
//older than 7 days
else if (new Date($(this).data('date')).getTime() < new Date().getTime() - (24 * 7) * 60 * 60 * 1000) {
$(this).css({
opacity: 0.5,
border: '2px solid red'
});
}
//7 days to go
else if (new Date($(this).data('date')).getTime() < new Date().getTime() + (24 * 7) * 60 * 60 * 1000) {
$(this).css({
opacity: 0.5,
border: '2px solid green'
});
}
});
你好,不要紧紧跟着你。 _“如果是在过去,然后如果少于7天,然后如果少于7天” _ 的代码是实现1日龄那么7天的并且将来日期 – Kulerbox