好吧,我試着在這裏遵循示例,我知道可能有幾種不同的方法將對象添加到localstorage中的數組,但不覆蓋它,但我至少沒有找到其中之一。將對象添加到localStorage中的數組
得到這個代碼來存儲數組中的對象,但它覆蓋自己。任何人都可以告訴我我錯過了什麼? (我恐怕我會錯過很多)。
function addEntry() {
var entryTitle = document.getElementById("entryTitle").value;
var entryText = document.getElementById("entryText").value;
var entry = {
"title": entryTitle,
"text": entryText
};
localStorage.setItem("entry", JSON.stringify(entry));
var allEntries = [];
allEntries.push(entry);
localStorage.setItem("allEntries", JSON.stringify(allEntries));
};
當然它改寫了自己你在localStorage的值設置爲你只是把東西到一個新的數組。你從哪裏獲得當前從localStorage存儲的內容? –
請解釋「它正在覆蓋自己」以及你正在試圖用你的代碼完成什麼。 – David