-1

我想将身体的背景更改为白色,当它设置为“太阳”(灯光模式)并变回月亮(也将背景更改为黑色)

function dwmode(){
    var body = document.querySelector('body')
    document.getElementById('dWmode').innerHTML = '<i class="fas fa-sun"></i>';
        if(innerHTML == '<i class="fas fa-sun"></i>'){
            body.style.background = 'white'
    }
}
4

1 回答 1

0

body = document.querySelector("body");
sun = document.getElementById("sun");
moon = document.getElementById("moon");

function dwmode() {
  if (sun.style.visibility == "visible") {
    body.style.backgroundColor = "white"
    sun.style.visibility = "hidden";
    moon.style.visibility = "visible";
  } else {
    body.style.backgroundColor = "gray"
    sun.style.visibility = "visible";
    moon.style.visibility = "hidden";
  }



}
body {
  background-color: gray;
}

.sunv {
  visibility: visible;
}

.moonv {
  visibility: hidden;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet" />



<i class="fas fa-sun sunv" id="sun" onclick="dwmode()"></i>
<i class="fas fa-moon moonv" id="moon" onclick="dwmode()"></i>

于 2021-05-23T18:38:41.247 回答