HTML DOM Storage key() 方法


HTML DOM Storage key() 方法用于返回存储对象中特定索引处的键名。 该索引作为 key() 方法的参数传递。 存储对象可以是会话对象或 localStorage 对象。

语法

以下是语法 −

使用 localStorage 的 Storage key() 方法 −

localStorage.key(index);

使用 sessionStorage 的 Storage key() 方法 −

sessionStorage.key(index);

这里,index 是整数类型,表示你要获取其名称的键号。

范例

我们来看一下 Storage key() 方法的范例 −

<!DOCTYPE html>
<html>
<body>
<h1 style="text-align:center">storage key() method example</h1>
<p>Get the first object key name by clicking on the below button</p>
<button onclick="keyName()">GET NAME</button>
<p id="Sample"></p>
<script>
   function keyName() {
      var k = localStorage.key(1);
      document.getElementById("Sample").innerHTML ="The key name of the second
      localStorage object is "+k;
   }
</script>
</body>
</html>

输出

这将生成以下输出 −

在点击 GET NAME 按钮时 −

更新于: 20-Aug-2019

92 次浏览

开启你的 职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.