HTML DOM 存储长度属性

htmlweb developmentfront end technology更新于 2024/5/26 17:48:17

HTML DOM 存储长度属性用于获取浏览器存储对象中存在的项目数量。存储对象可以是 localStorage 对象或 sessionStorage 对象。

语法

以下是语法 −

使用 localStorage 对象的存储长度属性 −

localStorage.length;

使用 sessionStorage 对象的存储长度属性

sessionStorage.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>

输出

这将产生以下输出 −

点击获取号码 −


相关文章


有用资源