【发布时间】:2019-08-09 09:17:48
【问题描述】:
已保存的本地存储值,但如何使用已保存的本地存储。我尝试创建一个函数 apply 但该类没有应用
document.querySelectorAll("#abc, #xyz").forEach(btn => {
btn.addEventListener("click", () => {
const e = document.querySelectorAll(".dog,.cat, #abc, #xyz");
e.forEach(el => {
el.classList.toggle("red");
var xyz = document.getElementById("xyz").className;
localStorage.setItem("vovo", xyz);
});
});
})
function apply(){
var xy = localStorage.getItem('vovo');
if (xy) {
var single = document.querySelectorAll(".dog,.cat, #abc, #xyz");
single.forEach(el => {
el.classList.add(xy);
});
}
};
【问题讨论】:
-
与其问“如何做”的问题,不如向我们展示你的尝试,并就问题所在提出一个具体的问题。
-
sori about it.. 更新到我尝试过的内容
-
哪个输入?以及你以后将如何使用它?
-
每次用户点击 .dog,.cat, #abc, #xyz,class/value "red" 将被保存到本地存储.. 所以当他们重新访问时,最后保存的 class/值将被应用
-
@Origami 加了答案,看看吧。
标签: javascript storage local