--- title: Storage.length slug: Web/API/Storage/length translation_of: Web/API/Storage/length ---
{{APIRef("Web Storage API")}}
length 是 {{domxref("Storage")}} 接口的只读属性,返回一个整数,表示存储在 Storage 对象里的数据项(data items)数量。
var aLength = storage.length;
一个整数。
下面的函数添加三个数据项到当前域名的本地存储里面,然后返回本地存储里面数据项的数量:
function populateStorage() {
localStorage.setItem('bgcolor', 'yellow');
localStorage.setItem('font', 'Helvetica');
localStorage.setItem('image', 'cats.png');
localStorage.length; // 返回 3
}
备注: 关于实际的例子,可以查看 Web Storage Demo.
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('Web Storage', '#dom-storage-length', 'length')}} | {{Spec2('Web Storage')}} |