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>

输出

将产生以下输出:

点击获取数量按钮后:

更新时间:2021-02-19

108 次浏览

启动你的 职业生涯

完成课程取得认证

开始
广告
© . All rights reserved.