时间:2023-05-29 18:39:01 | 来源:网站运营
时间:2023-05-29 18:39:01 来源:网站运营
HTML5(三)——Web 本地存储:WebStorage的目的是克服由cookie所带来的一些限制,当数据需要被严格控制在客户端时,不需要持续的将数据发回服务器。localStorage.user = "倩倩"localStorage.age = "18"localStorage.job = "打杂"console.log(localStorage)// {job: "打杂", age: "18", user: "倩倩", length: 3}for(key in localStorage){ console.log(`${key}--${localStorage[key]}`)}
运行程序之后,结果如图:localStorage.user = "倩倩"localStorage.age = "18"localStorage.job = "打杂"console.log(localStorage)// {job: "打杂", age: "18", user: "倩倩", length: 3}for(let i=0;i<localStorage.length;i++){ let key = localStorage.key(i) console.log(`${key}:${localStorage[key]}`)}
此时运行结果就是我们需要的结果了!<textarea name="" id="email" cols="30" rows="10" oninput="save()"></textarea><script> function save(){ var x = document.getElementById("email") localStorage.setItem('email',x.value) } window.onload =function(){ var x = document.getElementById("email") x.value = localStorage.getItem("email") }</script>
注意:如果你是直接使用浏览器打开html文件,此时发现并不会存储,需要声明下存储是针对域的,所以我们需要放到服务内,服务内访问才可以进行缓存。关键词:本地