HBuilder制作的app应用程序在手机端存储数据的应用,数据存储在本地,重启应用或关开机后数据不丢失。
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<script src="json/storage.json"></script>
<title></title>
</head>
<body>
name:<input id="name" type="text" size="6" /><br />
sex: <input id="sex" type="text" size="6"/><br />
tel: <input id="tel" type="text" size="6"/><br />
<input type="button" onclick="save()" value="存储" />
<input type="button" onclick="extract()" value="提取"/>
<textarea id="history" style="width: 340px;height: 500px;font-size: 16px;"></textarea>
<script>
function save(){
var obj=new Object();
var name=document.getElementById("name").value;
var sex=document.getElementById("sex").value;
var tel=document.getElementById("tel").value;
obj.name=name;
obj.sex=sex;
obj.tel=tel;
user=obj;
if(JSON.parse(localStorage.getItem("historys"))!=null&&JSON.parse(localStorage.getItem("historys"))!="")
historys=JSON.parse(localStorage.getItem("historys"));
else
historys=new Array();
historys[historys.length]=user;
console.log("user: "+user.name+" "+user.sex+" "+user.tel);
console.log("history: "+historys[historys.length-1].name+" "+historys[historys.length-1].sex+" "+historys[historys.length-1].tel);
localStorage.setItem("historys",JSON.stringify(historys));
}
function extract(){
historys=JSON.parse(localStorage.getItem("historys"));
for(var i=0;i<historys.length;i++)
document.getElementById("history").innerHTML+=historys[i].name+" "+historys[i].sex+" "+historys[i].tel+"\n";
}
</script>
</body>
</html>
JSON:
var user=new Array();
var historys;
PC页面效果:
安卓端运行结果: