此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Storage.clear()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

Storage 接口的 clear() 方法清除给定 Storage 对象中存储的所有键。

语法

js
clear()

参数

无。

返回值

无 (undefined)。

示例

下面的函数在本地存储里面创建三个数据项,然后使用 clear() 把它们全部移除。

js
function populateStorage() {
  localStorage.setItem("bgcolor", "red");
  localStorage.setItem("font", "Helvetica");
  localStorage.setItem("image", "miGato.png");

  localStorage.clear();
}

备注: 有关实际运行的例子,详见 Web Storage 演示

规范

Specification
HTML
# dom-storage-clear-dev

浏览器兼容性

相关链接

使用 Web Storage API