我创建了一个简单的 html 页面来测试本地存储,但在我的一生中,我无法让它工作,即使我的 Firebug 显示本地存储存储的值,因为我在这里存储的值是我的代码第一个 html 页面名为 Testhtml.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>TEsthtml.html</title>
</head>
<script >
function save()
{
var passvalue = document.getElementById('entry_1').value;
localStorage.setItem('passingvalue', passvalue);
}
</script>
<body>
<form action="simple.html" method="POST">
<label for="entry_0">Type ur name</label>
<input type="text" name="entry.0.single" value="" id="entry_0">
<br>
<label for="entry_1">ur pets name
</label>
<label for="entry_1">type something</label>
<input type="text" name="entry.1.single" value="" id="entry_1">
<br>
<input type="submit" name="submit" id="submit" onClick="save()" value="Submit">
</form>
</body>
</html>
第二种形式称为simple.html代码如下
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Simple HTML</title>
<script >
function load()
{
var storedvalue = localstorage.getItem('passingvalue');
if(storedvalue)
{
document.getElementById('tb1').value=storedValue;
}
}
</script>
</head>
<body onload="load()" >
<input id="tb1" name="tb1" type="text"/>
<input type="submit" name="submit" id="submit" onClick="load()" value="Submit">
</body>
</html>
我尝试在 simple.html 中运行此代码,这是唯一文本框中的第二种形式,它没有显示任何内容..尝试了很多不同的事情!!
最佳答案
这只是 Simple.html 第 8 行和第 9 行的打印错误; “localstorage”应为“localStorage”,“storedvalue”应为“storedValue”。
您应该查看浏览器的错误报告和开发人员工具,它们会帮助您自行解决此问题。我个人更喜欢在 Google Chrome 上进行开发,因为它的调试工具快速、简单且易于使用(如果您使用 Chrome,只需按 Ctrl+Shift+J)。
关于html - 为什么我的 HTML 5 本地存储无法工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8617520/