点击时通过类名更改底部边框,并设置深色模式开/关。

huangapple go评论75阅读模式
英文:

Onclick change border-bottom to a div by classname and set dark mode on/off

问题

我有这个暗模式的JavaScript,它完美地改变了背景颜色和文本字体颜色,但我需要它也能改变div类名.veritasborder-bottom,但这一部分不起作用。

这是代码:

var toggleBtn = document.getElementById("toggleBtn");
var body = document.getElementsByTagName("body")[0];
var borderBottom = document.getElementById(".veritas");

toggleBtn.addEventListener("click", function() {
  var bgColor, textColor;

  if (body.classList.contains("dark-mode")) {
    body.classList.remove("dark-mode");
    toggleBtn.textContent = "Dark mode is on";
    bgColor = "#FFFFFF";
    textColor = "#101010";
    borderBottom = "#101010";

  } else {
    body.classList.add("dark-mode");
    toggleBtn.textContent = "Dark mode is off";
    bgColor = "#101010";
    textColor = "#FFFFFF";
    borderBottom = "#FFFFFF";
  }

  body.style.backgroundColor = bgColor;
  body.style.color = textColor;
  borderBottom.style.borderBottom = borderBottom;

  // Save the chosen colors to localStorage
  localStorage.setItem("bgColor", bgColor);
  localStorage.setItem("textColor", textColor);
  localStorage.setItem("borderBottom", borderBottom);
});

// Retrieve saved colors from localStorage
var savedBgColor = localStorage.getItem("bgColor");
var savedTextColor = localStorage.getItem("textColor");
var savedBordersColor = localStorage.getItem("borderBottom");

if (savedBgColor && savedTextColor) {
  body.style.backgroundColor = savedBgColor;
  body.style.color = savedTextColor;
  borderBottom.style.borderBottom = borderBottom;
  if (savedBgColor === "#101010" && savedTextColor === "#FFFFFF") {
    body.classList.add("dark-mode");
    toggleBtn.textContent = "Dark mode is off";
  }
}
body {
  margin: 0;
  height: 100%;
}

.dark-mode {
  background-color: #101010;
  color: #FFFFFF;
}

.veritas {
  width: 90%;
  border-bottom: 1px solid #101010;
}
<button id="toggleBtn">Dark mode is on</button>
<div class="veritas">
  border-bottom should change color in dark/light mode
</div>

为什么这不会改变.veritas类名的border-bottom的颜色?

英文:

I have this dark mode JavaScript and it changes the background color and text font color perfect but I need it to also change the border-bottom of the div class name .veritas and that part is not working

Here is the code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var toggleBtn = document.getElementById(&quot;toggleBtn&quot;);
var body = document.getElementsByTagName(&quot;body&quot;)[0];
var borderBottom = document.getElementById(&quot;.veritas&quot;);
toggleBtn.addEventListener(&quot;click&quot;, function() {
var bgColor, textColor;
if (body.classList.contains(&quot;dark-mode&quot;)) {
body.classList.remove(&quot;dark-mode&quot;);
toggleBtn.textContent = &quot;Dark mode is on&quot;;
bgColor = &quot;#FFFFFF&quot;;
textColor = &quot;#101010&quot;;
borderBottom = &quot;#101010&quot;;
} else {
body.classList.add(&quot;dark-mode&quot;);
toggleBtn.textContent = &quot;Dark mode is off&quot;;
bgColor = &quot;#101010&quot;;
textColor = &quot;#FFFFFF&quot;;
borderBottom = &quot;#FFFFFF&quot;;
}
body.style.backgroundColor = bgColor;
body.style.color = textColor;
borderBottom.style.borderBottom = borderBottom;
// Save the chosen colors to localStorage
localStorage.setItem(&quot;bgColor&quot;, bgColor);
localStorage.setItem(&quot;textColor&quot;, textColor);
localStorage.setItem(&quot;borderBottom&quot;, borderBottom);
});
// Retrieve saved colors from localStorage
var savedBgColor = localStorage.getItem(&quot;bgColor&quot;);
var savedTextColor = localStorage.getItem(&quot;textColor&quot;);
var savedBordersColor = localStorage.getItem(&quot;borderBottom&quot;);
if (savedBgColor &amp;&amp; savedTextColor) {
body.style.backgroundColor = savedBgColor;
body.style.color = savedTextColor;
borderBottom.style.borderBottom = borderBottom;
if (savedBgColor === &quot;#101010&quot; &amp;&amp; savedTextColor === &quot;#FFFFFF&quot;) {
body.classList.add(&quot;dark-mode&quot;);
toggleBtn.textContent = &quot;Dark mode is off&quot;;
}
}

<!-- language: lang-css -->

body {
margin: 0;
height: 100%;
}
.dark-mode {
background-color: #101010;
color: #FFFFFF;
}
.veritas {
width: 90%;
border-bottom: 1px solid #101010;
}

<!-- language: lang-html -->

&lt;button id=&quot;toggleBtn&quot;&gt;Dark mode is on&lt;/button&gt;
&lt;div class=&quot;veritas&quot;&gt;
border-bottom should change color in dark/light mode
&lt;/div&gt;

<!-- end snippet -->

Any idea why this does not change the border-bottom in the div class name .veritas?

I created a Codepen to see it in action
https://codepen.io/familias/pen/gOQMrjX

答案1

得分: 0

  1. .veritas 是一个类,而你正在通过以下方式访问它:
borderBottom = document.getElementById(".veritas");

请将其更改为使用 className:

var borderBottom = document.getElementsByClassName("veritas")[0];

或者使用 querySelector:

var borderBottom = document.querySelector(".veritas");
  1. .borderBottom.style.borderBottom = borderBottom; 这里应该包含 widthstylecolor,而你只提供了颜色,将所有的以下出现:
borderBottom = "#FFFFFF";

更改为:

borderBottom = "1px solid #FFFFFF";
英文:

There are a couple of errors

1 .veritas is a class and you are access it using

borderBottom=document.getElementById(&quot;.veritas&quot;);. 

Change it to using className

var borderBottom=document.getElementsByClassName(&quot;veritas&quot;)[0];

or querySelector

var borderBottom=document.querySelector(&quot;.veritas&quot;);

2 . borderBottom.style.borderBottom = borderBottom; this should contain width style color, while you are only supplying color,
change all the occurrences of

borderBottom = &quot;#FFFFFF&quot;;

to

borderBottom = &quot;1px solid #FFFFFF&quot;;

答案2

得分: 0

以下是您要翻译的代码部分:

var toggleBtn = document.getElementById("toggleBtn");
var body = document.getElementsByTagName("body")[0];
var borderDiv = document.getElementsByClassName("veritas")[0];

toggleBtn.addEventListener("click", function() {
  var bgColor, textColor, borderBottom;

  if (body.classList.contains("dark-mode")) {
    body.classList.remove("dark-mode");
    toggleBtn.textContent = "Dark mode is on";
    bgColor = "#FFFFFF";
    textColor = "#101010";
    borderBottom = "#101010";
  } else {
    body.classList.add("dark-mode");
    toggleBtn.textContent = "Dark mode is off";
    bgColor = "#101010";
    textColor = "#FFFFFF";
    borderBottom = "#FFFFFF";
  }
  body.style.backgroundColor = bgColor;
  body.style.color = textColor;
  borderDiv.style.borderBottomColor = borderBottom;
});
body {
  margin: 0;
  height: 100%;
}

.dark-mode {
  background-color: #101010;
  color: #FFFFFF;
}

.veritas {
  width: 90%;
  border-bottom: 1px solid #101010;
}
<button id="toggleBtn">Dark mode is on</button>
<div class="veritas">
  border-bottom should change color in dark/light mode
</div>
英文:

It works like this

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var toggleBtn = document.getElementById(&quot;toggleBtn&quot;);
var body = document.getElementsByTagName(&quot;body&quot;)[0];
var borderDiv=document.getElementsByClassName(&quot;veritas&quot;)[0];
toggleBtn.addEventListener(&quot;click&quot;, function() {
var bgColor, textColor,borderBottom;
if (body.classList.contains(&quot;dark-mode&quot;)) {
body.classList.remove(&quot;dark-mode&quot;);
toggleBtn.textContent = &quot;Dark mode is on&quot;;
bgColor = &quot;#FFFFFF&quot;;
textColor = &quot;#101010&quot;;
borderBottom = &quot;#101010&quot;;
} else {
body.classList.add(&quot;dark-mode&quot;);
toggleBtn.textContent = &quot;Dark mode is off&quot;;
bgColor = &quot;#101010&quot;;
textColor = &quot;#FFFFFF&quot;;
borderBottom = &quot;#FFFFFF&quot;;
}
body.style.backgroundColor = bgColor;
body.style.color = textColor;
borderDiv.style.borderBottomColor = borderBottom;
});

<!-- language: lang-css -->

body {
margin:0;
height:100%;
}
.dark-mode {
background-color: #101010;
color: #FFFFFF;
}
.veritas{width:90%;
border-bottom:1px solid #101010;
}

<!-- language: lang-html -->

&lt;button id=&quot;toggleBtn&quot;&gt;Dark mode is on&lt;/button&gt;
&lt;div class=&quot;veritas&quot;&gt;
border-bottom should change color in dark/light mode
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月19日 22:52:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定