简体   繁体   English

通过更改暗模式切换,我还想更改引导程序 class

[英]By changing the dark mode toggle, I also want to change the bootstrap class

enter image description here of bootstrap I want to make a 'darkmode' toggle using the light mode /dark mode button.在此处输入引导程序的图像描述我想使用亮模式/暗模式按钮进行“暗模式”切换。

<button type="button" class="btn btn-dark">Dark Mode</button>

press this button按下这个按钮

class="btn btn-dark">  >>>  class="btn btn-light">

As the class changes随着 class 的变化

<script>
        function darkMode(){
            var body =document.body;
            body.classList.toggle("dark-mode");

            var button =document.getElementById("button");
            if(button.innerHTML == "Dark Mode"){
                button.innerHTML = "Light Mode"
            } else {
                button.innerHTML = "Dark Mode";
                button.innerText
            }
        }
    </script>

I've made it up to here.我已经到这里了。

I want to change the class in bootstrap, how do I do that?我想在引导程序中更改 class,我该怎么做?

You can use .className like you did before.您可以像以前一样使用.className For example:例如:

function darkMode(){
    var body = document.body;
    body.classList.toggle("dark-mode");

    var button = document.getElementById("button");
    if(button.innerHTML == "Dark Mode"){
        button.innerHTML = "Light Mode";
        button.className = "btn btn-light";
    } else {
        button.innerHTML = "Dark Mode";
        button.className = "btn btn-dark";
    }
}

If this fixes your problem, you can mark it as correct.如果这解决了您的问题,您可以将其标记为正确。

If.toggle() is not working, can also use.addClass() and.removeClass().如果.toggle() 不起作用,也可以使用.addClass() 和.removeClass()。 Keep a boolean of the toggle value, remove the existing class, and add the class that corresponds to the boolean.保留一个toggle值的boolean,去掉已有的class,添加Z84E2C64F38F78BA3EA5C90对应的class。

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

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