简体   繁体   English

尝试在 javascript 中实现暗/亮模式

[英]Trying to implement dark/light mode in javascript

 document.getElementById("theme_change").onclick = function () { if (document.body.style.backgroundColor = "#000") { document.body.style.backgroundColor = "#f5deb3"; document.getElementsByTagName("a")[0].style.color = "#000"; document.getElementsByTagName("a")[0].style.borderColor = "#000"; document.getElementById("theme_change").innerText = "Light Theme"; } else if (document.body.style.backgroundColor = "#f5deb3") { document.body.style.backgroundColor = "#000"; document.getElementsByTagName("a")[0].style.color = "#f5deb3"; document.getElementsByTagName("a")[0].style.borderColor = "#f5deb3"; document.getElementById("theme_change").innerText = "Dark Theme"; } }
 <div id="buttons"> <a href="#" id="color_change" class="btn">Change Color</a> <a href="#" id="theme_change" class="btn">Light Theme</a> </div>

I'm new in JavaScript and for some reasons I need to create a switch button to change the theme of my webpage.我是 JavaScript 的新手,由于某些原因,我需要创建一个切换按钮来更改我的网页主题。

I'm using the document.getElementById and the getElementByTagName to do so.我正在使用 document.getElementById 和 getElementByTagName 这样做。

But my problem is that when I click on the button once,only the background change and only the color of one of my buttons changes.When I click again on the button that is supposed to switch between the themes nothing happen.但我的问题是,当我单击一次按钮时,只有背景发生变化,只有一个按钮的颜色发生变化。当我再次单击应该在主题之间切换的按钮时,什么也没有发生。

On this page I have two buttons that change some colors on the page, the first one works really good so i can't figure out why the second one doesn't.在这个页面上,我有两个按钮可以更改页面上的一些 colors,第一个效果非常好,所以我不知道为什么第二个没有。

As explained before I can click on the theme_changer button one time, when I click on it the background changes (#000 -> #f5deb3), the color of the first button changes to (#f5deb3 -> #000) and the text of the second button change to (Light Theme -> Dark Theme) but not his color, it stays with the same color (#f5deb3) and when I click on it again nothing happens.如前所述,我可以单击一次theme_changer 按钮,当我单击它时,背景会发生变化(#000 -> #f5deb3),第一个按钮的颜色会变为(#f5deb3 -> #000)并且文本第二个按钮变为(Light Theme -> Dark Theme)但不是他的颜色,它保持相同的颜色(#f5deb3),当我再次点击它时没有任何反应。

I tried changing the order of the lines in the JavaScript file to see if something was different and so to be able to see where the problem could be, but nothing changed.The same happened with the browser console, which returned no errors.我尝试更改 JavaScript 文件中的行顺序,以查看是否有不同之处,以便能够看到问题出在哪里,但没有任何改变。浏览器控制台也发生了同样的情况,它没有返回任何错误。

Thanks for your help and advice.感谢您的帮助和建议。

You need to set the original background color to the "Light Theme" background and change the = in your if statements to == .您需要将原始背景颜色设置为“浅色主题”背景,并将 if 语句中的=更改为== Also you are only setting the text color for the first element that has an a tag.此外,您只为具有a标签的第一个元素设置文本颜色。 Cahange your code to something like this:将您的代码更改为以下内容:

document.getElementById("theme_change").onclick = function () {
    if (document.body.style.backgroundColor == "#000") {
        document.body.style.backgroundColor = "#f5deb3";
        document.getElementsByTagName("a")[0].style.color = "#000";
        document.getElementsByTagName("a")[0].style.borderColor = "#000";
        document.getElementsByTagName("a")[1].style.color = "#000";
        document.getElementsByTagName("a")[1].style.borderColor = "#000";
        document.getElementById("theme_change").innerText = "Light Theme";
    } else if (document.body.style.backgroundColor = "#f5deb3") {
        document.body.style.backgroundColor == "#000";
        document.getElementsByTagName("a")[0].style.color = "#f5deb3";
        document.getElementsByTagName("a")[0].style.borderColor = "#f5deb3";
        document.getElementsByTagName("a")[1].style.color = "#f5deb3";
        document.getElementsByTagName("a")[1].style.borderColor = "#f5deb3";
        document.getElementById("theme_change").innerText = "Dark Theme";

    }
}

There are few drawbacks in your code.您的代码几乎没有缺点。

  1. document.body.style.backgroundColor gives a color string in the format of "rgb" not in "hex". document.body.style.backgroundColor以“rgb”格式而不是“hex”格式给出颜色字符串。
  2. While using conditions within if statements, always use == or === instead of = .if语句中使用条件时,请始终使用=====而不是=

Therefore, this code should work for you:因此,此代码应该适合您:

 document.getElementById("theme_change").onclick = function () { if (document.body.style.backgroundColor === "" || document.body.style.backgroundColor === "rgb(0, 0, 0)") { document.body.style.backgroundColor = "#f5deb3"; document.getElementsByTagName("a")[0].style.color = "#000"; document.getElementsByTagName("a")[0].style.borderColor = "#000"; document.getElementById("theme_change").innerText = "Light Theme"; } else if (document.body.style.backgroundColor === "rgb(245, 222, 179)") { document.body.style.backgroundColor = "#000"; document.getElementsByTagName("a")[0].style.color = "#f5deb3"; document.getElementsByTagName("a")[0].style.borderColor = "#f5deb3"; document.getElementById("theme_change").innerText = "Dark Theme"; } }
 <div id="buttons"> <a href="#" id="color_change" class="btn">Change Color</a> <a href="#" id="theme_change" class="btn">Light Theme</a> </div>

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

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