倒计时到期日和到期日

时间:2013-02-06 19:59:23

标签: jquery math

我正在逐步完成每个类的.days - 因为在截止日期之前添加一个倒计时数字。 EG:截止日期为2天 - 截止日期:2013年2月8日

我还希望它显示截止日期后的负数。

http://jsfiddle.net/infatti/XeqPT/#base

天数到期是错的,我不明白为什么?

<ul>
<li>Due in <span class="days-due"></span> days - Due Date is <span class="month">02</span>/<span class="day">08</span>/<span class="year">2013</span></li>
<li>Due in <span class="days-due"></span> days - Due Date is <span class="month">02</span>/<span class="day">10</span>/<span class="year">2013</span></li>
</ul>

function daysUntil(year, month, day) {
  var now = new Date(),
      dateEnd = new Date(year, month - 1, day), // months are zero-based
      days = (dateEnd - now) / 1000/60/60/24;   // convert milliseconds to days

  return Math.round(days);
}

var monthDue = $(this).next('.month').text();
var dayDue = $(this).next('.day').text();
var yearDue = $(this).next('.year').text();

$('.days-due').each(function(){
    $(this).text(daysUntil(yearDue, monthDue, dayDue));
});

1 个答案:

答案 0 :(得分:1)

您在上下文中的

$(this)是指window

next看着兄弟姐妹。你的跨度不是窗口的兄弟。

更改您的选择器。此外,您的代码只查看一组元素,但您的标记有两个。

试试这个

function daysUntil(year, month, day) {
    var now = new Date(),
        dateEnd = new Date(year, month - 1, day); // months are zero-based
    days = (dateEnd - now) / 1000 / 60 / 60 / 24; // convert milliseconds to days
    return Math.round(days);
}

$("li").each(function () {

    var monthDue = $(this).find('.month').text();
    var dayDue = $(this).find('.day').text();
    var yearDue = $(this).find('.year').text();

    $(this).find(".days-due").text(daysUntil(yearDue, monthDue, dayDue));

});

找到所有li元素,并循环执行计算。由于我们选择了li this更改的上下文。

相关问题