多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# html5接口 sessionStorage 支持IE8+ 火狐,chrome等 用于临时保存同一窗口(或标签页)的数据,在关闭窗口或标签页之后将会删除这些数据。 ``` <pre class="calibre14">``` sessionStorage<span class="token2">.</span><span class="token1">setItem</span><span class="token2">(</span><span class="token4">'testKey'</span><span class="token2">,</span><span class="token4">'这是一个测试的value值'</span><span class="token2">)</span><span class="token2">;</span> sessionStorage<span class="token2">[</span><span class="token4">'testKey'</span><span class="token2">]</span> <span class="token">=</span> <span class="token4">'这是一个测试的value值'</span><span class="token2">;</span> sessionStorage<span class="token2">.</span><span class="token1">getItem</span><span class="token2">(</span><span class="token4">'testKey'</span><span class="token2">)</span><span class="token2">;</span> sessionStorage<span class="token2">[</span><span class="token4">'testKey'</span><span class="token2">]</span><span class="token2">;</span> sessionStorage<span class="token2">.</span><span class="token1">setItem</span><span class="token2">(</span><span class="token4">'user'</span><span class="token2">,</span> JSON<span class="token2">.</span><span class="token1">stringify</span><span class="token2">(</span>userEntity<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> 存储对象 var userJsonStr <span class="token">=</span> JSON<span class="token2">.</span><span class="token1">parse</span><span class="token2">(</span>sessionStorage<span class="token2">.</span><span class="token1">getItem</span><span class="token2">(</span><span class="token4">'user'</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> sessionStorage<span class="token2">.</span><span class="token1">removeItem</span><span class="token2">(</span>string key<span class="token2">)</span> :将指定的键名<span class="token2">(</span>key<span class="token2">)</span>从 sessionStorage 对象中移除。 sessionStorage<span class="token2">.</span><span class="token1">clear</span><span class="token2">(</span><span class="token2">)</span> ``` ``` localStorage 即本地存储,可用于长久保存整个网站的数据,保存的数据没有过期时间,直到手动去除 数据比较大的临时保存方案。如在线编辑文章时的自动保存。多页面访问共同数据 ``` <pre class="calibre16">``` localStorage<span class="token2">.</span><span class="token1">setItem</span><span class="token2">(</span><span class="token4">'testKey'</span><span class="token2">,</span><span class="token4">'这是一个测试的value值'</span><span class="token2">)</span><span class="token2">;</span> localStorage<span class="token2">[</span><span class="token4">'testKey'</span><span class="token2">]</span> <span class="token">=</span> <span class="token4">'这是一个测试的value值'</span><span class="token2">;</span> localStorage<span class="token2">.</span><span class="token1">getItem</span><span class="token2">(</span><span class="token4">'testKey'</span><span class="token2">)</span><span class="token2">;</span> localStorage<span class="token2">[</span><span class="token4">'testKey'</span><span class="token2">]</span><span class="token2">;</span> localStorage<span class="token2">.</span><span class="token1">setItem</span><span class="token2">(</span><span class="token4">'user'</span><span class="token2">,</span> JSON<span class="token2">.</span><span class="token1">stringify</span><span class="token2">(</span>userEntity<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> var userJsonStr <span class="token">=</span> JSON<span class="token2">.</span><span class="token1">parse</span><span class="token2">(</span><span class="token2">(</span> localStorage <span class="token2">.</span><span class="token1">getItem</span><span class="token2">(</span><span class="token4">'user'</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> localStorage<span class="token2">.</span><span class="token1">removeItem</span><span class="token2">(</span>string key<span class="token2">)</span> localStorage<span class="token2">.</span><span class="token1">clear</span><span class="token2">(</span><span class="token2">)</span> ``` ```