HTML5 localStorage从价值中获取关键 [英] HTML5 localStorage getting key from value
问题描述
localStorage.getItem将检索值.
localStorage.getItem will retrive value.
和setItem将设置值.
and setItem will set value.
但是,如果我想知道哪个键与此特定值相关联?那么如何获取密钥.
But If I want to know which key was associated with this particular value? Then how to get Key of that.?
推荐答案
访问 Html5存储文档到了解更多详情.使用以下语法来设置和获取localstorage/sessionstorage中的值 用于存储会话的值
visit Html5 Storage Doc to get more details. Use the following syntax to set and get the values in localstorage/sessionstorage for storing values for a session
sessionStorage.getItem('key')
sessionStorage.setItem('key','value')
或使用永久存储值
localStorage.getItem('key')
localStorage.setItem('key','value')
您说您想知道值,但是您想要键,则可以使用该功能
and u said u want to know the value but you want key then you can use the function
localStorage.key(i);
或者您也可以遍历可用键并通过交叉检查值来获取所需键
or also you can loop through the available keys and get the desired key by cross checking the value
for(var i=0, len=localStorage.length; i<len; i++) {
var key = localStorage.key(i);
var value = localStorage[key];
if(value.equals(desired_value))
console.log(key + " => " + value);
}
这篇关于HTML5 localStorage从价值中获取关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!