From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/storage/clear/index.html | 121 +++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 files/zh-cn/web/api/storage/clear/index.html (limited to 'files/zh-cn/web/api/storage/clear') diff --git a/files/zh-cn/web/api/storage/clear/index.html b/files/zh-cn/web/api/storage/clear/index.html new file mode 100644 index 0000000000..02eab09559 --- /dev/null +++ b/files/zh-cn/web/api/storage/clear/index.html @@ -0,0 +1,121 @@ +--- +title: Storage.clear() +slug: Web/API/Storage/clear +translation_of: Web/API/Storage/clear +--- +

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

+ +

clear() 是 {{domxref("Storage")}} 接口的一个方法,调用它可以清空存储对象里所有的键值。

+ +

语法

+ +
storage.clear();
+ +

参数

+ +

无。

+ +

返回值

+ +

无。

+ +

示例

+ +

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

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

备注:一个实际的例子 Web Storage Demo

+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Storage', '#dom-storage-clear', 'clear()')}}{{Spec2('Web Storage')}} 
+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
localStorage43.5810.504
sessionStorage52810.504
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support2.1{{ CompatUnknown }}811iOS 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

-- cgit v1.2.3-54-g00ecf