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/getitem/index.html | 131 +++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 files/zh-cn/web/api/storage/getitem/index.html (limited to 'files/zh-cn/web/api/storage/getitem') diff --git a/files/zh-cn/web/api/storage/getitem/index.html b/files/zh-cn/web/api/storage/getitem/index.html new file mode 100644 index 0000000000..11869d49e2 --- /dev/null +++ b/files/zh-cn/web/api/storage/getitem/index.html @@ -0,0 +1,131 @@ +--- +title: Storage.getItem() +slug: Web/API/Storage/getItem +translation_of: Web/API/Storage/getItem +--- +

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

+ +

getItem() 作为 {{domxref("Storage")}} 接口的方法,接受一个键名(key name)作为参数,并返回对应键名的值(key's value)。

+ +

语法

+ +
var aValue = storage.getItem(keyName);
+
+ +

参数

+ +
+
keyName
+
一个包含键名的 {{domxref("DOMString")}}。
+
+ +

返回值

+ +

一个 {{domxref("DOMString")}},键名对应的值。如果键名不存在于存储中,则返回 null

+ +

示例

+ +

下面的函数从本地存储中获取三个数据项,然后使用他们在页面上设置自定义样式:

+ +
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);
+}
+ +
+

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

+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Storage', '#dom-storage-getitem', 'getItem()')}}{{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