我想检查键中是否有特殊值,例如:
if(localStorage.getItem('choice') == "simple")
{
arrayname= simple.slice();
}
else if (localStorage.getItem('choice') == "middle")
{
arrayname= middle.slice();
}
else
{
arrayname= difficult.slice();
}
希望有人能帮助我。
最佳答案
让我们dive into html5 :
Like other JavaScript objects, you can treat the localStorage object as an associative array. Instead of using the getItem() and setItem() methods, you can simply use square brackets. For example, this snippet of code:
var foo = localStorage.getItem("bar");
// ...
localStorage.setItem("bar", foo);
…could be rewritten to use square bracket syntax instead:
var foo = localStorage["bar"];
// ...
localStorage["bar"] = foo;
所以你可以这样做:
if(localStorage['choice'] == "simple")
{
//...
}
else if (localStorage['choice'] == "middle")
{
//...
}
else
{
//...
}
关于javascript - 检查本地存储值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26011532/