Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将多个预先指定的日期存储到localStorage?_Javascript_Jquery_Html_Local Storage_Onload - Fatal编程技术网

Javascript 如何将多个预先指定的日期存储到localStorage?

Javascript 如何将多个预先指定的日期存储到localStorage?,javascript,jquery,html,local-storage,onload,Javascript,Jquery,Html,Local Storage,Onload,我正在尝试将当前时间和日期存储在表中。当前,在页面加载时,我的代码会更改日期以反映当前时间 我的代码如下所示: function currentDate() { var d = new Date(); return d.toString(); } window.onload = function() { localStorage.setItem("date", currentDate()); $('#current-location').prepend('<tr<

我正在尝试将当前时间和日期存储在表中。当前,在页面加载时,我的代码会更改日期以反映当前时间

我的代码如下所示:

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>');
}
函数currentDate(){
var d=新日期();
返回d.toString();
}
window.onload=函数(){
setItem(“日期”,currentDate());

$('#current location')。prepend('是的,您可以使用一个数组来实现这一点,只需继续将日期推送到数组中,如下所示

function currentDate() {
  var d = new Date();
  return d.toString();
}

var arr = JSON.parse(localStorage.getItem("date") || "[]");

arr.push(currentDate())

localStorage.setItem("date", JSON.stringify(arr));

arr.forEach(function(item) {
  $('#current-location').prepend('<tr><td>' + item + '</td></tr>');
});
函数currentDate(){
var d=新日期();
返回d.toString();
}
var arr=JSON.parse(localStorage.getItem(“日期”)| |“[]”);
arr.push(currentDate())
setItem(“date”,JSON.stringify(arr));
arr.forEach(功能(项目){
$(“#当前位置”)。前置(“”+项+“”);
});

因此,我将添加一个对象作为JSON,并将其字符串化版本保存到localStorage中。作为概念证明,您可以使用以下内容:

1) 在localStorage中初始化对象:

var listOfDates = {
  dates: []
};
localStorage.setItem('myDates', JSON.stringify(listOfDates));
addDateRowToTable = function() {

  var listOfDates = JSON.parse(localStorage.getItem('myDates'));
  listOfDates.dates.push(currentDate());
  localStorage.setItem('myDates', JSON.stringify(listOfDates));

  for(var i=0; i< listOfDates.dates.length; i++) {
   // do whatever you need to
  }
}
根据您的需要,您可能希望将其放入“if(!localStorage.getItem('myDates')”

2) 创建一个函数,用于读取存储、解析JSON、将一项添加到日期数组并保存回localStorage:

var listOfDates = {
  dates: []
};
localStorage.setItem('myDates', JSON.stringify(listOfDates));
addDateRowToTable = function() {

  var listOfDates = JSON.parse(localStorage.getItem('myDates'));
  listOfDates.dates.push(currentDate());
  localStorage.setItem('myDates', JSON.stringify(listOfDates));

  for(var i=0; i< listOfDates.dates.length; i++) {
   // do whatever you need to
  }
}
addDateRowtTable=function(){
var listOfDates=JSON.parse(localStorage.getItem('myDates');
listOfDates.dates.push(currentDate());
setItem('myDates',JSON.stringify(listOfDates));
对于(var i=0;i

我希望这会有所帮助。

每次加载页面时,js变量都会被重置。您需要将其存储在会话变量或cookies中。加载页面时,将日期添加到cookies中。在显示页面时,从cookies中获取日期。感谢您的解决方案。唯一的问题是抛出错误“Uncaught SyntaxError:来自“Sunday”中的“S”的意外标记S。真的吗?小提琴对你不起作用?它对我很好,我不确定日期的字符串如何在给定代码中抛出该错误?哦,等等,你可能没有清除本地存储,是吗?尝试从
“date”更改键“
到其他地方。哦,是的,成功了。谢谢!我还不知道所有的窍门。:)