在 localStorage 我有一个对象记录。它有很多字段记录[recordId],我希望能够删除这些条目。下面是我的删除功能,但它不起作用。所有记录仍然附加到本地存储中的记录对象。不知道出了什么问题,因为我正在以正确的方式删除。当我打印记录对象时,所有记录及其键仍然存在????
function deleteLocalRecord(recordId) {
var records = localStorage.getItem('records');
var theRecords;
if (supports_html5_storage()) {
if (records == null) {
theRecords = {};
} else {
// parse the records
theRecords = JSON.parse(records);
}
theRecords[recordId] = ''; // set the record to empty string.
delete theRecords[recordId]; // delete the record
// reset the records object in local storage after change
localStorage.setItem('records', JSON.stringify(theRecords));
}
}
function supports_html5_storage()
{
try
{
return 'localStorage' in window && window['localStorage'] !== null;
}
catch (e)
{
return false;
}
}