如何实现localStorage的存储功能?

更新于
2026-09-29 09:04:10
1阅读来源:SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计542个文字,预计阅读时间需要3分钟。

如何实现localStorage的存储功能?

1. [代码] [JavaScript] 代码 + head title test / title / head body input type=text id=local input type=button id=clear value=clear script type=text/javascript // ![CDATA[ (function() { window.localData={ hname: location.hostname }; })(); // ]]> /script /body

如何实现localStorage的存储功能?

1.[代码][JavaScript]代码

<html> <head> <title>test</title> </head> <body> <input type="text" id="local" > <input type="button" id="clear" value="clear"> <script type="text/javascript"> // <![CDATA[ (function(){ window.localData = { hname:location.hostname?location.hostname:'localStatus', isLocalStorage:window.localStorage?true:false, dataDom:null, initDom:function(){ if(!this.dataDom){ try{ this.dataDom = document.createElement('input'); this.dataDom.type = 'hidden'; this.dataDom.style.display = "none"; this.dataDom.addBehavior('#default#userData'); document.body.appendChild(this.dataDom); var exDate = new Date(); exDate = exDate.getDate()+30; this.dataDom.expires = exDate.toUTCString(); }catch(ex){ return false; } } return true; }, set:function(key,value){ if(this.isLocalStorage){ window.localStorage.setItem(key,value); }else{ if(this.initDom()){ this.dataDom.load(this.hname); this.dataDom.setAttribute(key,value); this.dataDom.save(this.hname) } } }, get:function(key){ if(this.isLocalStorage){ return window.localStorage.getItem(key); }else{ if(this.initDom()){ this.dataDom.load(this.hname); return this.dataDom.getAttribute(key); } } }, remove:function(key){ if(this.isLocalStorage){ localStorage.removeItem(key); }else{ if(this.initDom()){ this.dataDom.load(this.hname); this.dataDom.removeAttribute(key); this.dataDom.save(this.hname) } } } } var text = document.getElementById('local'); var btn = document.getElementById('clear'); window.onbeforeunload = function(){ localData.set('beiyuuData',text.value); } btn.onclick = function(){localData.remove('beiyuuData');text.value=''}; if(localData.get('beiyuuData')){ text.value = localData.get('beiyuuData'); } })() // ]]> </script> </body> </html>

2.[文件] storage.html~3KB 下载(1)

<html> <head> <title>test</title> </head> <body> <input type="text" id="local" > <input type="button" id="clear" value="clear"> <script type="text/javascript"> // <![CDATA[ (function(){ window.localData = { hname:location.hostname?location.hostname:'localStatus', isLocalStorage:window.localStorage?true:false, dataDom:null, initDom:function(){ if(!this.dataDom){ try{ this.dataDom = document.createElement('input'); this.dataDom.type = 'hidden'; this.dataDom.style.display = "none"; this.dataDom.addBehavior('#default#userData'); document.body.appendChild(this.dataDom); var exDate = new Date(); exDate = exDate.getDate()+30; this.dataDom.expires = exDate.toUTCString(); }catch(ex){ return false; } } return true; }, set:function(key,value){ if(this.isLocalStorage){ window.localStorage.setItem(key,value); }else{ if(this.initDom()){ this.dataDom.load(this.hname); this.dataDom.setAttribute(key,value); this.dataDom.save(this.hname) } } }, get:function(key){ if(this.isLocalStorage){ return window.localStorage.getItem(key); }else{ if(this.initDom()){ this.dataDom.load(this.hname); return this.dataDom.getAttribute(key); } } }, remove:function(key){ if(this.isLocalStorage){ localStorage.removeItem(key); }else{ if(this.initDom()){ this.dataDom.load(this.hname); this.dataDom.removeAttribute(key); this.dataDom.save(this.hname) } } } } var text = document.getElementById('local'); var btn = document.getElementById('clear'); window.onbeforeunload = function(){ localData.set('beiyuuData',text.value); } btn.onclick = function(){localData.remove('beiyuuData');text.value=''}; if(localData.get('beiyuuData')){ text.value = localData.get('beiyuuData'); } })() // ]]> </script> </body> </html>

本文共计542个文字,预计阅读时间需要3分钟。

如何实现localStorage的存储功能?

1. [代码] [JavaScript] 代码 + head title test / title / head body input type=text id=local input type=button id=clear value=clear script type=text/javascript // ![CDATA[ (function() { window.localData={ hname: location.hostname }; })(); // ]]> /script /body

如何实现localStorage的存储功能?

1.[代码][JavaScript]代码

<html> <head> <title>test</title> </head> <body> <input type="text" id="local" > <input type="button" id="clear" value="clear"> <script type="text/javascript"> // <![CDATA[ (function(){ window.localData = { hname:location.hostname?location.hostname:'localStatus', isLocalStorage:window.localStorage?true:false, dataDom:null, initDom:function(){ if(!this.dataDom){ try{ this.dataDom = document.createElement('input'); this.dataDom.type = 'hidden'; this.dataDom.style.display = "none"; this.dataDom.addBehavior('#default#userData'); document.body.appendChild(this.dataDom); var exDate = new Date(); exDate = exDate.getDate()+30; this.dataDom.expires = exDate.toUTCString(); }catch(ex){ return false; } } return true; }, set:function(key,value){ if(this.isLocalStorage){ window.localStorage.setItem(key,value); }else{ if(this.initDom()){ this.dataDom.load(this.hname); this.dataDom.setAttribute(key,value); this.dataDom.save(this.hname) } } }, get:function(key){ if(this.isLocalStorage){ return window.localStorage.getItem(key); }else{ if(this.initDom()){ this.dataDom.load(this.hname); return this.dataDom.getAttribute(key); } } }, remove:function(key){ if(this.isLocalStorage){ localStorage.removeItem(key); }else{ if(this.initDom()){ this.dataDom.load(this.hname); this.dataDom.removeAttribute(key); this.dataDom.save(this.hname) } } } } var text = document.getElementById('local'); var btn = document.getElementById('clear'); window.onbeforeunload = function(){ localData.set('beiyuuData',text.value); } btn.onclick = function(){localData.remove('beiyuuData');text.value=''}; if(localData.get('beiyuuData')){ text.value = localData.get('beiyuuData'); } })() // ]]> </script> </body> </html>

2.[文件] storage.html~3KB 下载(1)

<html> <head> <title>test</title> </head> <body> <input type="text" id="local" > <input type="button" id="clear" value="clear"> <script type="text/javascript"> // <![CDATA[ (function(){ window.localData = { hname:location.hostname?location.hostname:'localStatus', isLocalStorage:window.localStorage?true:false, dataDom:null, initDom:function(){ if(!this.dataDom){ try{ this.dataDom = document.createElement('input'); this.dataDom.type = 'hidden'; this.dataDom.style.display = "none"; this.dataDom.addBehavior('#default#userData'); document.body.appendChild(this.dataDom); var exDate = new Date(); exDate = exDate.getDate()+30; this.dataDom.expires = exDate.toUTCString(); }catch(ex){ return false; } } return true; }, set:function(key,value){ if(this.isLocalStorage){ window.localStorage.setItem(key,value); }else{ if(this.initDom()){ this.dataDom.load(this.hname); this.dataDom.setAttribute(key,value); this.dataDom.save(this.hname) } } }, get:function(key){ if(this.isLocalStorage){ return window.localStorage.getItem(key); }else{ if(this.initDom()){ this.dataDom.load(this.hname); return this.dataDom.getAttribute(key); } } }, remove:function(key){ if(this.isLocalStorage){ localStorage.removeItem(key); }else{ if(this.initDom()){ this.dataDom.load(this.hname); this.dataDom.removeAttribute(key); this.dataDom.save(this.hname) } } } } var text = document.getElementById('local'); var btn = document.getElementById('clear'); window.onbeforeunload = function(){ localData.set('beiyuuData',text.value); } btn.onclick = function(){localData.remove('beiyuuData');text.value=''}; if(localData.get('beiyuuData')){ text.value = localData.get('beiyuuData'); } })() // ]]> </script> </body> </html>