英文:
Get specific value from Localstorage key and populate div
问题
我有一个存储在localStorage中的键,我想获取特定的值并将其填充到一个元素中。
示例:
<div id="results"></div>
document.getElementById('results').innerHTML = localStorage.getItem('customer');
customer = {
"fullname": "John Doe",
"firstname": "John"
};
当然,这会输出整个键。我如何检索并显示只有fullname
的值?
英文:
I have a key in localStorage and I want to get specific value and populate an element with it.
Example:
<div id="results"></div>
document.getElementById('results').innerHTML = localStorage.getItem('customer');
customer = {
"fullname": "John Doe",
"firstname": "John"
};
This of course outputs the full key. How do I retrieve and display just the value of fullname
?
答案1
得分: 1
你可以使用JSON.parse()
来解析字符串值并获取你需要的键:
var customer = JSON.parse(localStorage.getItem('customer'));
var fullname = customer.fullname;
document.getElementById('results').innerHTML = fullname;
英文:
You can use JSON.parse()
to parse the string value and get the key you need:
var customer = JSON.parse(localStorage.getItem('customer'));
var fullname = customer.fullname;
document.getElementById('results').innerHTML = fullname;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论