【发布时间】:2015-06-01 16:12:16
【问题描述】:
我有一个日历。日历中的事件以 JSON 格式编写并存储在 localStorage 中,如下所示:
key = events //All events as Object
value =
{
"Date":"31-3-2015",
"Event":"Football match",
"Participants":"Italy - Germany",
"Description":"Support our team with your friends!"},
"Date":"2-4-2015",
"Event":"First-night play",
"Participants":"Famous actors",
"Description":"Going out to the theatre with my gf"}
etc...
我还可以更改日历中事件的每一行。但是我需要一个新值来存储在 localStorage 中。
例如
//Old version
""Date":"2-4-2015",
"Event":"First-night play",
"Participants":"Famous actors",
"Description":"Going out to the theatre with my gf"}
//Edited version
"Date":"2-4-2015",
"Event":"First-night play",
"Participants":"Famous actors",
"Description":"Let my gf visit the play alone"}
这是我添加新事件或编辑现有事件的功能:
var addNewEvent = function() {
var events = JSON.parse(localStorage.getItem('events')) || [];
events.push(
{Date: storageNewDate,
Event: storageEvent,
Participants: storageParticipants,
Description: storageDescription});
localStorage.setItem('events', JSON.stringify(events));
};
但它仅适用于 localStorage(如果您决定用新事件替换现有事件,它不会重置 localStorage),但在视觉上您会在日历单元格中看到您的新事件。
那么,在我的情况下,我如何参考 localStorage 中的“描述”(“事件”?“参与者”?...)来重新设置值?
【问题讨论】:
标签: javascript json html local-storage