存储
接口在
Web 存储 API
provides access to a particular domain's session or local storage. It allows, for example, the addition, modification, or deletion of stored data items.
To manipulate, for instance, the session storage for a domain, a call to
Window.sessionStorage
is made; whereas for local storage the call is made to
Window.localStorage
.
Storage.length
只读
存储
对象。
Storage.key()
n
, this method will return the name of the nth key in the storage.
Storage.getItem()
When passed a key name, will return that key's value.
Storage.setItem()
When passed a key name and value, will add that key to the storage, or update that key's value if it already exists.
Storage.removeItem()
When passed a key name, will remove that key from the storage.
Storage.clear()
When invoked, will empty all keys out of the storage.
Here we access a
存储
object by calling
localStorage
. We first test whether the local storage contains data items using
!localStorage.getItem('bgcolor')
. If it does, we run a function called
setStyles()
that grabs the data items using
Storage.getItem()
and uses those values to update page styles. If it doesn't, we run another function,
populateStorage()
, which uses
Storage.setItem()
to set the item values, then runs
setStyles()
.
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);
}
注意 : To see this running as a complete working example, see our Web 存储演示 .
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'Storage' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
存储
|
Chrome 4 | Edge 12 | Firefox 3.5 | IE 8 | Opera 10.5 | Safari 4 | WebView Android ≤37 | Chrome Android 18 | Firefox Android 6 | Opera Android 11 | Safari iOS 3.2 | Samsung Internet Android 1.0 |
clear
|
Chrome 4 | Edge 12 | Firefox 3.5 | IE 8 | Opera 10.5 | Safari 4 | WebView Android Yes | Chrome Android 18 | Firefox Android 6 | Opera Android 11 | Safari iOS 3.2 | Samsung Internet Android 1.0 |
getItem
|
Chrome 4 | Edge 12 | Firefox 3.5 | IE 8 | Opera 10.5 | Safari 4 | WebView Android ≤37 | Chrome Android 18 | Firefox Android 6 | Opera Android 11 | Safari iOS 3.2 | Samsung Internet Android 1.0 |
key
|
Chrome 4 | Edge 12 | Firefox 3.5 | IE 8 | Opera 10.5 | Safari 4 | WebView Android Yes | Chrome Android 18 | Firefox Android 6 | Opera Android 11 | Safari iOS 3.2 | Samsung Internet Android 1.0 |
length
|
Chrome 4 | Edge 12 | Firefox 3.5 | IE 8 | Opera 10.5 | Safari 4 | WebView Android Yes | Chrome Android 18 | Firefox Android 6 | Opera Android 11 | Safari iOS 3.2 | Samsung Internet Android 1.0 |
removeItem
|
Chrome 4 | Edge 12 | Firefox 3.5 | IE 8 | Opera 10.5 | Safari 4 | WebView Android ≤37 | Chrome Android 18 | Firefox Android 6 | Opera Android 11 | Safari iOS 3.2 | Samsung Internet Android 1.0 |
setItem
|
Chrome 4 | Edge 12 | Firefox 3.5 | IE 8 | Opera 10.5 | Safari 4 | WebView Android ≤37 | Chrome Android 18 | Firefox Android 6 | Opera Android 11 | Safari iOS 3.2 | Samsung Internet Android 1.0 |
完整支持
存储