要检索存储在浏览器本地存储中的数据,应使用localStorage.getItem()方法。以下是示例代码:
// 存储数据 localStorage.setItem('username', 'John');
// 检索数据 var username = localStorage.getItem('username');
// 确认数据存在 if (username) { console.log('Username: ', username); } else { console.log('No username found'); }
在检索存储数据时,请确保使用正确的键。如果键不存在,则getItem()方法将返回null。如果您有任何疑问,请参阅浏览器文档并仔细阅读代码。
上一篇:本地存储数据返回未定义或空值
下一篇:本地存储数据未被渲染