【发布时间】:2015-12-07 03:05:19
【问题描述】:
我正在尝试将当前时间和日期存储在表格中。目前,在页面加载时,我的代码会更改日期以反映当前时间。
我的代码如下所示:
function currentDate() {
var d = new Date();
return d.toString();
}
window.onload = function() {
localStorage.setItem("date", currentDate());
$('#current-location').prepend('<tr<td>'+localStorage.getItem("date")+'</td>');
}
我试过console.log(localStorage),所以我知道那里保存了一个日期。但是,我想存储重新加载页面的日期(比如第二次加载页面并出现 2 个日期等)我需要一个数组吗?如果是这样,我将如何将数组内容添加到表中?
【问题讨论】:
-
每次加载页面时,js 变量都会被重置。您需要将其存储在会话变量或 cookie 中。加载页面时,将日期添加到 cookie。显示时从 cookie 中获取它。
标签: javascript jquery html local-storage onload