javascript - 如何正确使用本地存储?
问题描述
我正在制作一个将其值保存在多个页面上的变量,并且我正在使用 localstorage 来执行此操作。问题是它不保存变量,也没有给出错误。
在发布此内容之前我尝试过做什么:我在 localstorage 上搜索了 youtube 教程我尝试使用此网站https://www.taniarascia.com/how-to-use-local-storage-with-javascript/ 我搜索了它在stackoverflow上
我找不到这个问题的答案。
变量.js 文件
var mode = "light";
localStorage.setItem('mode', '');
function draw(){
if (document.body.style.backgroundColor == "#181818"){
mode = "dark";
console.log(mode);
}
if (document.body.style.backgroundColor == "#f2f112" || mode == "dark"){
mode = "dark";
alert("Error while attempting to change background to dark theme.")
}
}
index.html 文件
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<p><a href="settings.html">Settings</a></p>
<script type="text/javascript"></script>
<script src="variables.js"></script>
<script src="settings.html"></script>
<script>
modeselect = localStorage.getItem(mode);
if (modeselect == "light"){
console.log("light mode");
document.body.style.backgroundColor = "#f2f112";
}
if (modeselect == "dark"){
console.log("dark mode");
document.body.style.backgroundColor = "#f2f112";
}
</script>
</body>
</html>
settings.html 文件
<!DOCTYPE html>
<html>
<head>
<title>Settings</title>
</head>
<style>
.button1 {
background-color: #444444;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
.button2 {
background-color: #ffffff;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
body {
background-color: rgb(156,158,158);
}
</style>
<body>
<form>
<input type="button" class="button1" value="Dark mode" onclick="darkmode()">
<input type="button" class="button2" value="Light mode" style ="color:black;" onclick="lightmode()">
</form>
<p><a href="index.html">Home</a></p>
<script src="variables.js"></script>
<script>
localStorage.getItem('mode');
function darkmode() {
mode = "dark";
alert(mode);
if (mode == "light"){
alert("error while changing theme");
}
if (mode == "dark"){
alert("successfully changed to dark mode");
document.body.style.backgroundColor = "#181818";
}
}
function lightmode() {
mode = "light";
alert(mode);
if (mode == "light"){
alert("successfully changed to light mode");
document.body.style.backgroundColor = "#9c9e9e";
}
if (mode == "dark"){
alert("error while changing theme");
}
}
</script>
</body>
</html>
解决方案
您没有在本地存储中设置值。您设置的本地存储值为空。
使用值为“mode”的“localStorage.setItem”。例子:
localStorage.setItem('mode', mode);
要获得价值,请使用 localStorage.getItem('mode')
推荐阅读
- c++ - 为什么我的 .cc 和 .h 可以在我自己的 test.cc 上编译和运行,但不能在 TA 的代码上运行
- python - 我无法访问我创建的电影列表(python)
- sql - 试图找到乘以 DateDiff * 租金天数的语法,然后添加到应付金额列
- c# - 如何获取与经过身份验证的 Azure 服务主体关联的 ObjectID?
- python-3.x - 找不到页面 (404) django.contrib.admin.options.add_view
- sql - MS Access 从宽格式到长格式 新变量 Union ALL
- java - Java Swing:尝试隐藏和显示 JTable 的列不适用于动态宽度
- javascript - 在 React 中使用 @loadable/component 从外部脚本加载函数
- firebase - Firebase 函数调用者角色无法正常工作
- usb - STM32 USB编程,跳转到DFU的bootloader