相关疑难解决方法(0)

在HTML5 localStorage中存储对象

我想在HTML5中存储JavaScript对象localStorage,但我的对象显然正在转换为字符串.

我可以使用存储和检索原始JavaScript类型和数组localStorage,但对象似乎不起作用.他们应该吗?

这是我的代码:

var testObject = { 'one': 1, 'two': 2, 'three': 3 };
console.log('typeof testObject: ' + typeof testObject);
console.log('testObject properties:');
for (var prop in testObject) {
    console.log('  ' + prop + ': ' + testObject[prop]);
}

// Put the object into storage
localStorage.setItem('testObject', testObject);

// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');

console.log('typeof retrievedObject: ' + typeof retrievedObject);
console.log('Value of retrievedObject: ' + retrievedObject);
Run Code Online (Sandbox Code Playgroud)

控制台输出是

typeof testObject: object
testObject properties:
  one: 1 …
Run Code Online (Sandbox Code Playgroud)

javascript html5 local-storage

2386
推荐指数
14
解决办法
101万
查看次数

标签 统计

html5 ×1

javascript ×1

local-storage ×1