Javascript 如何在localstorage中保留对象数组
我正在尝试将localstorage用于我的对象数组,但如果没有localstorage,它将无法正常工作。经过几天的尝试,我在这里尝试。有人能帮忙吗? 守则:Javascript 如何在localstorage中保留对象数组,javascript,arrays,json,object,local-storage,Javascript,Arrays,Json,Object,Local Storage,我正在尝试将localstorage用于我的对象数组,但如果没有localstorage,它将无法正常工作。经过几天的尝试,我在这里尝试。有人能帮忙吗? 守则: <script> var notes = JSON.parse(localstorage.getitem("notes") || "[]"); function todo() { // Gets invoked by a submit
<script>
var notes = JSON.parse(localstorage.getitem("notes") || "[]");
function todo() { // Gets invoked by a submit button
var list = document.getElementById("tasklist").value;
var taskdate = document.getElementById("taskdate").value;
var tasktime = document.getElementById("tasktime").value;
const note = {
list: list,
taskdate: taskdate,
tasktime: tasktime
}
notes.push(note);
for (i = 0; i < notes.length; i++) {
document.getElementById("name").append(notes[i].list);
}
localStorage.setItem("notes", JSON.stringify(notes));
}
</script>
您使用了不正确的关键字 localstorage=>localstorage
getitem=>getitem除了一些语法错误之外,您的代码似乎有效。使用一个好的IDE或代码编辑器或任何东西来显示错误。我推荐
您需要localStorage.getItem而不是localStorage.getItem吗?与大多数其他语言一样,JavaScript与许多JavaScript属性和函数一样区分大小写。
try please declare as array, note variable
var note = array();
note = {
list: list,
taskdate: taskdate,
tasktime: tasktime
}
var notes = JSON.parse(localStorage.getItem("notes") || "[]");
function todo() { // Gets invoked by a submit button
var list = document.getElementById("tasklist").value;
var taskdate = document.getElementById("taskdate").value;
var tasktime = document.getElementById("tasktime").value;
const note = {
list: list,
taskdate: taskdate,
tasktime: tasktime
};
notes.push(note);
for (let i = 0; i < notes.length; i++) {
document.getElementById("name").append(notes[i].list);
}
localStorage.setItem("notes", JSON.stringify(notes));
}