繁体   English   中英

页面刷新后如何取消刷新开关按钮?

[英]How do I cancel the refresh switch button after a page refresh?

当我切换到另一章时,我需要有关如何取消切换按钮的刷新 position 的建议。

js主题切换代码:

document.addEventListener('DOMContentLoaded', () => {
if(localStorage.darkTheme === 'true'){
    document.body.classList.add('dark');
}
else{document.body.classList.remove('dark');};
const themeSwitch = document.getElementById('themeCheckBox');
themeSwitch.addEventListener('change', () => {    
    if (document.body.classList.contains('dark')){
        localStorage.darkTheme = 'false';
    }else { localStorage.darkTheme = 'true';};
    document.body.classList.toggle('dark');  
});

您的 javascript 代码应该像这样更改。 您在触发DOMContentLoaded事件后设置了复选框的值。 并根据复选框值addremove class。

document.addEventListener('DOMContentLoaded', () => {
    const themeSwitch = document.getElementById('themeCheckBox');

    if(localStorage.darkTheme === 'true'){
        document.body.classList.add('dark');
        themeSwitch.checked = true;
    } else{
        document.body.classList.remove('dark');
        themeSwitch.checked = false;
    }

    themeSwitch.addEventListener('change', () => {    
        if (themeSwitch.checked){
            localStorage.darkTheme = 'true';
            document.body.classList.add('dark');
        } else {
            localStorage.darkTheme = 'false';
            document.body.classList.remove('dark');
        }  
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM