簡體   English   中英

javascript 和網站上的用戶輸入問題以進行暗模式切換

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

如您所見,我有一個 javascript 應該在網站上進行暗模式切換。 javascript 工作我收到警報,但是當我按下開關時沒有任何反應。 為什么? 注意:我對編程和其他東西非常陌生,所以對白痴的解釋會很棒。 ----example.html----

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

---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----(主要來自css)

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

你有一個非常有趣的錯誤

而不是if (darkmode = 0)

應該是if (darkmode === 0)

僅此而已

你的 js 中有一個錯誤,當你比較時你應該放==而不是= 所以你的 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