您需要采取一些步骤将这些信息正确存储在localStorage中。但是,在开始编写代码之前,请注意,localStorage(当前) 不能
保存除字符串以外的任何数据类型。您将需要序列化阵列进行存储,然后将其解析回去以对其进行修改。
步骤1:
仅当尚未将序列化数组存储在localStoragesession
变量中时,才应运行下面的第一个代码段。
为确保您的localStorage正确设置并存储阵列,请首先运行以下代码段:
var a = [];a.push(JSON.parse(localStorage.getItem('session')));localStorage.setItem('session', JSON.stringify(a));以上代码仅应运行 一次, 并且仅当您尚未将 数组 存储在localStorage
session变量中时才应运行。如果您已经在执行此操作,请跳至步骤2。
第2步:
像这样修改您的功能:
function SaveDataToLocalStorage(data){ var a = []; // Parse the serialized data back into an aray of objects a = JSON.parse(localStorage.getItem('session')) || []; // Push the new data (whether it be an object or anything else) onto the array a.push(data); // alert the array value alert(a); // Should be something like [Object array] // Re-serialize the array back into a string and store it in localStorage localStorage.setItem('session', JSON.stringify(a));}这应该为您处理其余的工作。当您解析它时,它将成为一个对象数组。
希望这可以帮助。



