[英]Saving images to local storage
I'm working on a project that has two stylesheets to choose - dark and light. 我正在一个有两个样式表可供选择的项目-深色和浅色。
So, what my code does - it changes stylesheet's href tag and two small icons src (logo and some icon). 因此,我的代码做了什么-它更改了样式表的href标签和两个小图标src(徽标和一些图标)。 Then it saves recently used stylesheet to local storage.
然后将最近使用的样式表保存到本地存储。
function ChangeMode() {
var image = document.getElementById("mode");
var logo = document.getElementById("logo");
if (image.src.match("day_mode")) {
image.src = "night_mode_s.png";
logo.src = "logo_black.png";
document.getElementById("stylesheet").setAttribute('href', 'main_dark.css');
} else {
image.src = "day_mode_s.png";
logo.src = "logo.png";
document.getElementById("stylesheet").setAttribute('href', 'main.css');
}
}
if (localStorage.getItem("ModeChanger")) {
document.getElementById("stylesheet").setAttribute("href", localStorage.getItem("ModeChanger"));
}
function MyStylesheet() {
var x = document.getElementById("stylesheet").getAttribute("href");
document.getElementById("stylesheet").setAttribute("href", x);
localStorage.setItem("ModeChanger", x);
}`
What i need to do now is to save image's src to local storage. 我现在需要做的是将图像的src保存到本地存储。 I tried to change keys and values in function MyStylesheet, but Chrome says that something is wrong.
我试图更改函数MyStylesheet中的键和值,但是Chrome表示出了点问题。
My question is - are there different rules when it comes to saving images to local storage? 我的问题是-将图像保存到本地存储时是否有不同的规则? What seems to be logical to me is to use this, slightly changed function
在我看来,合乎逻辑的是使用此功能,但功能略有更改
var y = document.getElementById("mode").getAttribute("src");
document.getElementById("mode").setAttribute("src", y);
localStorage.setItem("ModeChangerTwo", y);
And then refer to it in function above. 然后在上面的函数中引用它。
Send help. 发送帮助。
You can only storage Strings in LocalStorage, try this: 您只能在本地存储中存储字符串,请尝试以下操作:
localStorage.setItem("ModeChangerTwo", JSON.stringify(y));
Assuming that your variable contains the url property or is the object reference and modeChangerTwo will be its key. 假设您的变量包含url属性或是对象引用,而modeChangerTwo将是其键。
and to obtain the value use: 并获得价值使用:
y = JSON.parse(localStorage.getItem('ModeChangerTwo'));
to convert it again in a Javascript object. 再次将其转换为Javascript对象。
Thank you all for your help especially you, @Chris G. Your method seemed to be the easiest one, so I used it. 谢谢大家的帮助,尤其是@ChrisG。您的方法似乎是最简单的方法,所以我使用了它。
I made another function that didn't use local stogage at all. 我做了另一个根本不使用局部笔触的函数。 It looks like this, and it works!
看起来像这样,并且有效!
window.addEventListener("load", function() {
var x = document.getElementById("stylesheet").getAttribute("href");
var y = document.getElementById("mode");
var z = document.getElementById("logo");
if (document.getElementById("stylesheet").getAttribute("href") == "main_dark.css") {
y.src = "night_mode_s.png";
z.src = "logo_black.png";
}
else {
y.src = "day_mode_s.png";
z.src = "logo.png";
}});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.