[英]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.