【问题标题】:How do I cancel the refresh switch button after a page refresh?页面刷新后如何取消刷新开关按钮?
【发布时间】:2021-04-18 22:22:09
【问题描述】:

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

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');  
});

【问题讨论】:

  • 您可以使用markdown添加网站的链接。 [litecode.net](http://litecode.net)。发布问题时请遵循正确的格式。 : D. 发帖时要具体。您的主题开关的js 代码是什么?
  • 请更新您的问题,以便在minimal, complete, and verifiable example 中显示您的相关代码。让我们知道what you have tried so far 以及什么不起作用。更多信息请参考how to ask good questions的帮助文章。
  • @NimnaPerera 我在帖子中添加了主题更改代码
  • @wazz 我想问一下如何取消开关按钮上的刷新?

标签: javascript html css web


【解决方案1】:

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

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');
        }  
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 2013-11-01
    • 2019-08-22
    相关资源
    最近更新 更多