例:今天是4月查找元素最接近的日期,然後添加類
2012年12月
2012年12月
月標題(2012年12月5日)
標題(7。 2012年12月)
- 2012年12月
最接近的日期是12月5日(不3.December)(更新不早)
而且如果人數超過一 「12月5日」,所以添加類只有第一胎
HTML:
<div class="wrap">
<div class="zone" id="one">
<div class="box">
<footer class="time">1. December 2012</footer>
</div>
<div class="box">
<footer class="time">1. December 2012</footer>
</div>
<div class="box">
<footer class="time">3. December 2012</footer>
</div>
<div class="box">
<h2>Title <span class="time">(5. December 2012)</span></h2>
</div>
<div class="box">
<h2>Title <span class="time">(7. December 2012)</span></h2>
</div>
<div class="box">
<footer class="time">9. December 2012</footer>
</div>
</div>
<div class="zone" id="two">
<!-- Same .zone#one but i will focus for .zone#one only-->
</div>
</div>
<code></code>
的jQuery:
var closest = [];
$('.wrap > .zone:eq(0) .box').each(function(i) {
var date = $(this).find(".time").html().replace("(","").split(".");
closest.push(date[0]);
});
$("code").html(closest+"");
遊樂場:http://jsfiddle.net/WJvZb/
我走到現在這一步,但也沒辦法找到最接近的日期,並添加類它(例如。 .closest
class)
我想你想要做的是這裏計算的日期,你會得到一個整數回以毫秒爲單位,你計算每一個日期,每次它比小舊的你替換它。你會替換第一個ofc,只要檢查你的當前值是否爲0(或-1是否安全),如果它大於你剛剛計算的值。 –