【发布时间】:2017-11-16 02:24:08
【问题描述】:
我已经使用添加到我元素的数据属性中的把手进行了倒计时,但我想要添加当前年份。我使用的框架倒计时是接受“2017/06/14”这种数据倒计时格式的最终倒计时,我的 html 是这样的:
<div class="cd" id="cd" data-countdown="{{month}}/{{day}} {{time}}"></div>
我想要实现的是将第一个索引中的年份添加为
data-countdown="2017/{{month}}/{{day}} {{time}}"
到目前为止,这是我的 js 脚本:
var currentDate = new Date().getFullYear();
$('.cd').each(function(){
//get each data-attribute
var a = $(this).data("countdown");
var b = a.split(" ");
//add the current year to it
b.unshift(currentDate);
c = b[0]+'\/'+b[1]+'\/'+b[2];
$(this).data("countdown", c); });
【问题讨论】:
-
如果您使用
.attr(),您的代码可以正常工作,例如$(this).attr("data-countdown", c); -
非 jquery 答案:
this.dataset.countdown = c;- 相当于上述 -
@CarstenLøvboAndersen 出错,无法将
11/6 08:00:00转换为日期对象。我还将 currentDate 转换为字符串仍然不起作用 -
@JaromandaX 它也不起作用。我想我应该只使用把手让用户输入当前年份,但这对他们没有帮助吧?
-
所以,甚至
c = currentDate + '/' + a;都没有(注意,无需转义/)
标签: javascript jquery jquery-countdown