学习html5第三天

时间:2022-11-13 20:53:01

web存储

1.localstorage没有时间限制的存储。

2.sessionstorage 针对session的

    <textarea id="tacontent" style="width:200px; height:200px;"></textarea>
<button id="btnsave">save</button>

 <script>
window.onload=function (){
var ta=document.getElementById("tacontent");
var btn=document.getElementById("btnsave");
if(localStorage.text){
ta.value=localStorage.text;
}
btn.onclick=function (){
localStorage.text=ta.value;

}
}

</script>

 <script>
var btn=document.getElementById("btnsave");
var ta=document.getElementById("tacontent");
window.onload=function (){
if(localStorage.text){
ta.value=localStorage.text;
}
btn.click=funcation (){
localStorage.text=ta.value;

}
}

</script>

2.sessionstorage 针对session的存储。

<span id="sp_num">0</span>
<button id="btn_add">add</button>

 <script>
var num,btn_add,sp_num;
window.onload=function(){
num=0;
btn_add=document.getElementById("btn_add");
sp_num=document.getElementById("sp_num");
if(sessionStorage.nyum){
num=sessionStorage.nyum;

}else{
num=0;
}

btn_add.onclick=function(){
num++;
sessionStorage.nyum=num;
showNum();
}
function showNum(){
sp_num.innerHTML=num;

}
}

</script>