<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<input type="text" id="demo" />
<p id="text"></p>
<button type="button" onClick="savestorage()" value="">保存数据</button>
<button type="button" onClick="loadstorage()" value="">读取数据</button>
<script>
function savestorage()
{
var textValue=document.getElementById("demo").value;
localStorage.setItem("message",textValue);
}
function loadstorage()
{
var tar=document.getElementById("text");
var msg=localStorage.getItem("message");/*注意此处是getItem*/
tar.innerHTML=msg;
}
</script>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<input type="text" id="demo" />
<p id="text"></p>
<button type="button" onClick="savestorage()" value="">保存数据</button>
<button type="button" onClick="loadstorage()" value="">读取数据</button>
<script>
function savestorage()
{
var textValue=document.getElementById("demo").value;
localStorage.setItem("message",textValue);
}
function loadstorage()
{
var tar=document.getElementById("text");
var msg=localStorage.getItem("message");/*注意此处是getItem*/
tar.innerHTML=msg;
}
</script>
</body>
</html>

本文介绍了如何使用HTML和JavaScript创建一个简单的网页应用,通过使用localStorage对象来实现数据的本地保存和读取功能。演示了如何通过按钮触发保存和读取操作,方便用户在不同页面间保持数据的一致性和连续性。
2451

被折叠的 条评论
为什么被折叠?



