--- title: Storage.removeItem() slug: Web/API/Storage/removeItem translation_of: Web/API/Storage/removeItem ---

{{APIRef("Web Storage API")}}

{{domxref("Storage")}} 接口的 removeItem() 方法,接受一个键名作为参数,会从给定的Storage对象中删除该键名(如果存在)。 如果没有与该给定键名匹配的项,则此方法将不执行任何操作。

语法

storage.removeItem(keyName);

参数

keyName
一个 {{domxref("DOMString")}},即你想要移除的键名。

返回值

无。

示例

下面的函数在本地存储里面创建三个数据项,然后把 image 数据项移除。

function populateStorage() {
  localStorage.setItem('bgcolor', 'red');
  localStorage.setItem('font', 'Helvetica');
  localStorage.setItem('image', 'myCat.png');

  localStorage.removeItem('image');
}

备注:完整的例子,可查看 Web Storage Demo

规范

Specification Status Comment
{{SpecName('Web Storage', '#dom-storage-removeitem', 'removeItem()')}} {{Spec2('Web Storage')}}  

浏览器兼容性

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
localStorage 4 3.5 8 10.50 4
sessionStorage 5 2 8 10.50 4
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 2.1 {{ CompatUnknown }} 8 11 iOS 3.2

各浏览器支持的 localStorage 和 sessionStorage 大小上限不同。测试页面 detailed rundown of all the storage capacities for various browsers

Note: since iOS 5.1, Safari Mobile stores localStorage data in the cache folder, which is subject to occasional clean up, at the behest of the OS, typically if space is short.

相关链接

使用 Web Storage API