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