gpt4 book ai didi

javascript - 希望获得不断变化的 LocalStorage 值

转载 作者:行者123 更新时间:2023-11-28 19:26:41 27 4
gpt4 key购买 nike

希望我能以一种有意义的方式解释我的问题。

我在单击按钮时更改 CSS 主题,当单击特定主题的按钮时,它会将该类名保存到 LocalStorage,但由于 LocalStorage 键和值不断变化,我只想调用任何单击后在 localstorage 中设置,而不是特定值。我正在使用 jQuery 进行按钮点击,我在下面包含了我的代码,我们将不胜感激任何帮助。

$(document).ready(function(){

$('#dark').click(function(){
$('#app-root').removeClass();
$('#app-root').addClass('theme-dark');
localStorage.clear();
localStorage.setItem('theme-dark', ['theme-dark']);
localStorage.getItem('theme-dark');
});

$('#light').click(function(){
$('#app-root').removeClass();
$('#app-root').addClass('theme-light');
localStorage.clear();
localStorage.setItem('theme-light', ['theme-light']);
localStorage.getItem('theme-dark');
});

$('#beaker').click(function(){
$('#app-root').removeClass();
$('#app-root').addClass('theme-beaker');
localStorage.clear();
localStorage.setItem('theme-beaker', ['theme-beaker']);
localStorage.getItem('theme-dark');
});

$('#outrun').click(function(){
$('#app-root').removeClass();
$('#app-root').addClass('theme-outrun');
localStorage.clear();
localStorage.setItem('theme-outrun', ['theme-outrun']);
localStorage.getItem('theme-dark');
});

var localItem = localStorage.getItem('theme-outrun');

$('body').addClass(localItem);

});

最佳答案

一键设置本地存储的不同主题

$(document).ready(function(){

$('#dark').click(function(){
$('#app-root').removeClass();
$('#app-root').addClass('theme-dark');
localStorage.clear();
localStorage.setItem('theme', ['theme-dark']);
localStorage.getItem('theme');
});

$('#light').click(function(){
$('#app-root').removeClass();
$('#app-root').addClass('theme-light');
localStorage.clear();
localStorage.setItem('theme', ['theme-light']);
localStorage.getItem('theme');
});

$('#beaker').click(function(){
$('#app-root').removeClass();
$('#app-root').addClass('theme-beaker');
localStorage.clear();
localStorage.setItem('theme', ['theme-beaker']);
localStorage.getItem('theme');
});

$('#outrun').click(function(){
$('#app-root').removeClass();
$('#app-root').addClass('theme-outrun');
localStorage.clear();
localStorage.setItem('theme', ['theme-outrun']);
localStorage.getItem('theme');
});

var localItem = localStorage.getItem('theme');

$('body').addClass(localItem);

});

关于javascript - 希望获得不断变化的 LocalStorage 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55998725/

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