Storage.key()
key()
作为 Storage
接口的方法,接受一个数值 n 作为参数,返回存储对象第 n 个数据项的键名。键的存储顺序是由用户代理定义的,所以尽可能不要依赖这个方法。
语法
var aKeyName = storage.key(key);
参数
- key
- 一个整数,表示要获取的键名索引。
返回值
一个包含键名的 DOMString
。
示例
下面的函数添加三个数据项到当前域名的本地存储里,然后返回第三个的键名:
function populateStorage() {
localStorage.setItem('bgcolor', 'yellow');
localStorage.setItem('font', 'Helvetica');
localStorage.setItem('image', 'cats.png');
localStorage.key(2); // 应该返回 'image'
}
备注: 关于实际的例子,可以查看 Web Storage Demo.
规范
Specification | Status | Comment |
---|---|---|
Unknown key() |
Unknown |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help! (en-US)
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 | ? | 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.