--- title: Storage slug: Web/API/Storage tags: - API - Interface - Reference - Storage - Web Storage - data translation_of: Web/API/Storage --- <p>{{APIRef("Web Storage API")}}</p> <p>作为 Web Storage API 的接口,<strong><code>Storage</code> </strong>提供了访问特定域名下的会话存储或本地存储的功能,例如,可以添加、修改或删除存储的数据项。</p> <p>如果你想要操作一个域名的会话存储,可以使用 {{domxref("Window.sessionStorage")}};如果想要操作一个域名的本地存储,可以使用 {{domxref("Window.localStorage")}}。</p> <h2 id="属性">属性</h2> <dl> <dt>{{domxref("Storage.length")}} {{readonlyInline}}</dt> <dd>返回一个整数,表示存储在 <code>Storage</code> 对象中的数据项数量。</dd> </dl> <h2 id="方法">方法</h2> <dl> <dt>{{domxref("Storage.key()")}}</dt> <dd>该方法接受一个数值 n 作为参数,并返回存储中的第 n 个键名。</dd> </dl> <dl> <dt>{{domxref("Storage.getItem()")}}</dt> <dd>该方法接受一个键名作为参数,返回键名对应的值。</dd> <dt>{{domxref("Storage.setItem()")}}</dt> <dd>该方法接受一个键名和值作为参数,将会把键值对添加到存储中,如果键名存在,则更新其对应的值。</dd> <dt>{{domxref("Storage.removeItem()")}}</dt> <dd>该方法接受一个键名作为参数,并把该键名从存储中删除。</dd> <dt>{{domxref("Storage.clear()")}}</dt> <dd>调用该方法会清空存储中的所有键名。</dd> </dl> <h2 id="示例">示例</h2> <p>这里我们通过调用 <code>localStorage</code> 来访问一个 <code>Storage</code> 对象。首先,使用 <code>!localStorage.getItem('bgcolor')</code> 测试本地存储中是否包含该数据项。如果包含,则运行 <code>setStyles()</code> 函数,该函数使用 <code>localStorage.getItem()</code> 来获取数据项,并使用这些值更新页面样式。如果不包含,我们运行另一个函数 <code>populateStorage()</code>,该函数使用 <code>localStorage.setItem()</code> 设置数据项,然后运行 <code>setStyles()</code>。</p> <pre class="brush: js">if(!localStorage.getItem('bgcolor')) { populateStorage(); } else { setStyles(); } function populateStorage() { localStorage.setItem('bgcolor', document.getElementById('bgcolor').value); localStorage.setItem('font', document.getElementById('font').value); localStorage.setItem('image', document.getElementById('image').value); setStyles(); } function setStyles() { var currentColor = localStorage.getItem('bgcolor'); var currentFont = localStorage.getItem('font'); var currentImage = localStorage.getItem('image'); document.getElementById('bgcolor').value = currentColor; document.getElementById('font').value = currentFont; document.getElementById('image').value = currentImage; htmlElem.style.backgroundColor = '#' + currentColor; pElem.style.fontFamily = currentFont; imgElem.setAttribute('src', currentImage); }</pre> <div class="note"> <p><strong>备注:</strong>要运行完整的例子,可查看 <a href="https://mdn.github.io/dom-examples/web-storage/">Web Storage Demo</a>。</p> </div> <h2 id="规范">规范</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> <tr> <td>{{SpecName('Web Storage', '#the-storage-interface', 'Storage')}}</td> <td>{{Spec2('Web Storage')}}</td> <td> </td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <p>{{Compat("api.Storage")}}</p> <h2 id="相关链接">相关链接</h2> <ul> <li><a href="/zh-CN/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API">使用 Web Storage API</a></li> <li>{{domxref("Window.localStorage")}}</li> <li>{{domxref("Window.sessionStorage")}}</li> <li>{{domxref("CacheStorage")}}</li> </ul>