HTML DOM Storage key() 方法
htmlweb developmentfront end technology更新于 2025/9/27 18:52:17
HTML DOM Storage key() 方法用于返回 Storage 对象中给定索引处的键名。索引作为参数传递给 key() 方法。Storage 对象可以是会话对象或 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>
输出
这将产生以下输出 −

点击"获取名称"按钮 −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

