我的问题是结果显示为[null,null,"something"]]
而不是"something"
。
我的代码如下所示:
var simpan = JSON.parse(localStorage.getItem('subject')) || [];
simpandata.push(subjek);
localStorage.setItem('subject', JSON.stringify(simpandata));
var showsubjek = localStorage["subject"];
showsubjek = JSON.stringify(showsubjek);
showsubjek2 = JSON.parse(showsubjek);
document.getElementById("getItemitu").innerHTML=showsubjek2;
然后,如果我删除此代码:showsubjek = JSON.stringify(showsubjek);
结果将显示为[object, object]...
答案 0 :(得分:1)
获取localStorage
值,如下所示(正确方式)。
错误的方式
var showsubjek = localStorage["subject"];
正确的方式
var showsubjek = localStorage.getItem("subject");