参见英文答案 >
Storing Objects in HTML5 localStorage25个
如何在html5中的localStorage对象中存储mycars数组?
如何在html5中的localStorage对象中存储mycars数组?
var mycars = new Array(); mycars[0] = "Saab"; mycars[1] = "Volvo"; mycars[2] = "BMW"; localStorage.mycars=?;
解决方法
localStorage是关键:值对,所以你可能想要做的是JSON.stringify数组,并将字符串存储在mycars键中,然后可以将其拉回来并将其复制到JSON.parse中.例如,
var mycars = new Array(); mycars[0] = "Saab"; mycars[1] = "Volvo"; mycars[2] = "BMW"; localStorage["mycars"] = JSON.stringify(mycars); var cars = JSON.parse(localStorage["mycars"]);