Javascript 保存暗模式复选框切换到本地存储
在我的网站上,我有一个复选框,可以在默认的浅色主题和深色主题之间切换。如果页面设置为暗模式并刷新,则主题将切换回亮模式。有没有办法保存访问者的决定,使页面在刷新时保持黑暗模式?这是我目前拥有的代码:Javascript 保存暗模式复选框切换到本地存储,javascript,html,css,web,local-storage,Javascript,Html,Css,Web,Local Storage,在我的网站上,我有一个复选框,可以在默认的浅色主题和深色主题之间切换。如果页面设置为暗模式并刷新,则主题将切换回亮模式。有没有办法保存访问者的决定,使页面在刷新时保持黑暗模式?这是我目前拥有的代码: constchk=document.getElementById('chk'); chk.addEventListener('change',()=>{ document.body.classList.toggle('dark'); }); 正文{ 背景:#f5; 颜色:#3535; } 身体黑{
constchk=document.getElementById('chk');
chk.addEventListener('change',()=>{
document.body.classList.toggle('dark');
});代码>
正文{
背景:#f5;
颜色:#3535;
}
身体黑{
背景色:#1A1A;
颜色:#F5;
}
网站文本
让darkMode=false
如果用户选中,则将变量更改为true
然后在本地存储器中设置变量
i、 e
setItem('darkTheme',true)
然后使用localStorage.getItem(darkTheme')获取变量这里是一个非常简单的示例。使用localStorage.setItem()
在本地存储中存储值。您可以使用任何方法从输入检索数据。这里我只是使用一个表单
函数切换DarkMode(){
const formElem=document.getElementById('dark-mode');
const formData=新formData(formElem);
常量darkModeState=!!formData.get('toggler');
setItem('darkMode',darkModeState);
}
使用暗码
拯救
本地存储和会话存储属性允许在web浏览器中保存键/值
对
localStorage对象存储没有过期日期的数据。当浏览器关闭时,数据将不会被删除,并将在第二天、第二周或第二年可用
要存储值,请选中要存储在本地存储器中的复选框。无论何时打开页面检查值存储与否
你的案子你试试这个
StackOverflow代码段注释存储本地存储,因此将此代码复制到您的文件。
const-toggleSwitch=document.querySelector('input[type=“checkbox”]”);
const currentTheme=localStorage.getItem('theme');
如果(当前主题){
document.documentElement.setAttribute('data-theme',currentTheme);
如果(currentTheme==='dark'){
toggleSwitch.checked=true;
document.body.classList.toggle('dark');
}
}
功能切换主题(e){
如果(例如,选中目标){
document.documentElement.setAttribute('data-theme','dark');
setItem('theme','dark');
document.body.classList.toggle('dark');
}
else{document.documentElement.setAttribute('data-theme','light');
setItem('theme','light');
document.body.classList.toggle('dark');
}
}
toggleSwitch.addEventListener('change',switchTheme,false)代码>
正文{
背景:#f5;
颜色:#3535;
}
身体黑{
背景色:#1A1A;
颜色:#F5;
}
网站文本
首先定义一个变量,并将localstorage定义为其值(最好在设置值之前检查本地存储),如下所示:
let DarkMode = localStorage.getItem('DarkMode');
if(DarkMode == null) {
localStorage.setItem('DarkMode', 'false');
}
然后,当用户选中该框时,将其设置为true
并在本地存储中进行更新
让DarkMode=localStorage.getItem('DarkMode');
if(暗模式==null){
setItem('DarkMode','false');
}
const chk=document.getElementById('chk');
chk.addEventListener('change',()=>{
document.body.classList.toggle('dark');
if(暗模式==false){
暗模式=真;
setItem('DarkMode',true)
log(暗模式);
}否则{
暗模式=假;
setItem('DarkMode',false)
log(暗模式);
}
});代码>
正文{
背景:#f5;
颜色:#3535;
}
身体黑{
背景色:#1A1A;
颜色:#F5;
}
网站文本
身体{
背景:#f5;
颜色:#3535;
文本对齐:右对齐;
}
身体黑{
背景色:#1A1A;
颜色:#F5;
}
本地存储的暗模式
打开暗模式
网站内容
const chk=document.getElementById('chk');
chk.addEventListener('click',()=>{
chk.checked?document.body.classList.add(“深色”):document.body.classList.remove(“深色”);
setItem('darkModeStatus',chk.checked);
});
window.addEventListener('load',(事件)=>{
if(localStorage.getItem('darkModeStatus')=“true”){
document.body.classList.add(“深色”);
document.getElementById('chk')。checked=true;
}
});
您是使用任何语言编程还是仅使用html?
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
background: #f5f5f5;
color: #353535;
text-align:right;
}
body.dark {
background-color: #1a1a1a;
color: #f5f5f5;
}
</style>
<title>Dark mode to Local Storage</title>
</head>
<body>
<div class="toggle">
<input type="checkbox" class="checkbox" id="chk" />
<label class="label" for="chk">Turn On Dark Mode</label>
</div>
<h1>Website Content</h1>
<script>
const chk = document.getElementById('chk');
chk.addEventListener('click', () => {
chk.checked?document.body.classList.add("dark"):document.body.classList.remove("dark");
localStorage.setItem('darkModeStatus', chk.checked);
});
window.addEventListener('load', (event) => {
if(localStorage.getItem('darkModeStatus')=="true"){
document.body.classList.add("dark");
document.getElementById('chk').checked = true;
}
});
</script>
</body>
</html>