我需要在同一个HTML ID中每24小时递增几个不同的数字。因此,如果一个数字是1,另一个是20,不管是什么数字,它都必须加1。
正如您所看到的,我只需要一种方法来将HTML ID中的任何数字递增+1,而不是将数字改为1。
<p id="pColor">
Flower are on day <span>(<span id="datePlus">1</span>)</span>
</p>
<p id="pColor">
Fruiting Plants are on day <span>(<span id="datePlus">20</span>)</span>
</p> JS
function theDate() {
var initialDate = new Date(2017, 0, 19); // Dec 1st 2012
var now = Date.now();
var difference = now - initialDate;
var millisecondsPerDay = 24 * 60 * 60 * 1000;
var daysSince = Math.floor(difference / millisecondsPerDay);
console.log(daysSince);
function dateUpdate() {
if(daysSince >=1) {
console.log("True");
// THIS IS WHERE I WANT CODE TO GO
} else {
console.log("false");
}
}
}
theDate();发布于 2017-01-20 14:56:05
首先你需要的是class,而不是id。
在html页面中,id不能重复。
并使用类增加每个元素的值。
// The .each() method is unnecessary here:
$( ".datePlus" ).each(function() {
$( this ).html( parseInt($( this ).html()) + 1);
});当这段代码运行时,它会在每个元素中将值加一。
发布于 2017-01-20 15:03:03
您可以使用jquery的for循环添加+1
for(var i = 1;i<7;i++)
{
$("#demo").append(i+"</br>");//just for testing
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="demo"></p>
发布于 2017-01-20 15:04:00
首先,将id="datePlus"更改为class="datePlus"。
然后使用此函数:
function theDate() {
var initialDate = new Date(2017, 0, 19);
var now = Date.now();
var difference = now - initialDate;
var millisecondsPerDay = 24 * 60 * 60 * 1000;
var daysSince = Math.floor(difference / millisecondsPerDay);
if(daysSince > 0) {
$(".datePlus").each(function(){
var $this = $(this);
var number = parseInt($this.text());
number = isNaN(number)? daysSince: number + daysSince;
$this.text(number);
});
}
}https://stackoverflow.com/questions/41757670
复制相似问题