【发布时间】:2015-06-17 02:40:11
【问题描述】:
这是我的 HTML 代码,我将 json 数组存储在本地存储中。我想稍后随时迭代 json,但它显示它未定义。如何使用 js 遍历存储的 json 数组?
<!DOCTYPE html>
<html>
<head>
<script>
var ob=[
{
"id":1,
"name":"one"
},
{
"id":2,
"name":"two"
}
];
function clickCounter() {alert("dsdsd");
if(typeof(Storage) !== "undefined") {
if (sessionStorage.ob) {
sessionStorage.ob = ob;
} else {
sessionStorage.ob = undefined;
}
alert(sessionStorage.ob[0].id);
for (var i in sessionStorage.ob)
{
alert(sessionStorage.ob[i].id);
}
} else {
}
}
</script>
</head>
<body>
<button onclick="clickCounter()" type="button">Click me!</button>
</body>
</html>
【问题讨论】:
标签: javascript json html session-storage