HTML DOM 存储 length 属性
HTML DOM 存储 length 属性用于获取浏览器存储对象中存在的项数。该存储对象可以是 localStorage 对象或 sessionStorage 对象。
语法
以下是语法:
使用 localStorage 对象的存储 length 属性:
localStorage.length;
使用 sessionStorage 对象的存储 length 属性
sessionStorage.length;
示例
让我们了解一下存储 length 属性的示例:
<!DOCTYPE html>
<html>
<body>
<h1 style="text-align:center">Storage length property example</h1>
<p>Get how many storage items are stored in the local storage object by clicking the below button</p>
<button onclick="itemNum()">GET NUMBER</button>
<p id="Sample"></p>
<script>
function itemNum() {
var num = localStorage.length;
document.getElementById("Sample").innerHTML = "Number of storage items are "+num;
}
</script>
</body>
</html>输出
将产生以下输出:

点击获取数量按钮后:

广告
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP