简体   繁体   English

javascript 和网站上的用户输入问题以进行暗模式切换

[英]Problem with javascript and user-input on website to make a darkmode switch

As you can see, I have a javascript which is supposed to make a darkmode switch on a website.如您所见,我有一个 javascript 应该在网站上进行暗模式切换。 javascript works I get the alert but nothing happens when I press the switch. javascript 工作我收到警报,但是当我按下开关时没有任何反应。 Why?为什么? Note: I am very new to programming and stuff, so explanations for an idiot would be great.注意:我对编程和其他东西非常陌生,所以对白痴的解释会很棒。 ----example.html---- ----example.html----

  <a href="#" class="right">
    <button type="button" onclick="func()">Darkmode</button>
  </a>

---script.js--- ---script.js---

let darkmode = 0
function func() {
    if(darkmode = 0){
        document.getElementById("main").style.background = "grey";
        document.getElementById("row").style.background = "grey";
        document.getElementById("footer").style.background = "grey";
        darkmode = 1;}
    else{
        document.getElementById("main").style.background = "white";
        document.getElementById("row").style.background = "white";
        document.getElementById("footer").style.background = "white";
        darkmode = 0;}
  }
alert("11 hot singles in your area");

----style.css---- (fe the main stuff from css) ----style.css----(主要来自css)

  /* Main column */
  .main {   
    -ms-flex: 70%; /* IE10 */
    flex: 70%;
    background-color: white;
    padding: 20px;
  }

You got a very funny bug there你有一个非常有趣的错误

instead of if (darkmode = 0)而不是if (darkmode = 0)

it should be if (darkmode === 0)应该是if (darkmode === 0)

and that is just it仅此而已

There is an error in your js that when you compare you should put == instead of = .你的 js 中有一个错误,当你比较时你应该放==而不是= SO your js will be:所以你的 js 将是:

 let darkmode = 0 function func() { if(darkmode == 0){ document.getElementById("main").style.background = "grey"; document.getElementById("row").style.background = "grey"; document.getElementById("footer").style.background = "grey"; darkmode = 1;} else{ document.getElementById("main").style.background = "white"; document.getElementById("row").style.background = "white"; document.getElementById("footer").style.background = "white"; darkmode = 0;} } alert("11 hot singles in your area");

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

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