gpt4 book ai didi

javascript - 检查本地存储值

转载 作者:行者123 更新时间:2023-12-03 11:46:56 25 4
gpt4 key购买 nike

我想检查键中是否有特殊值,例如:

    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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com