javascript - Javascript - 每次點擊,如何使黑暗模式切換到工作?

  显示原文与译文双语对照的内容

我在構建一個暗模式開關的應用程序,它在第一次點擊時起作用,但在那之後,它每次點擊都起作用。

這段代碼顯示了一個複選框。在項目中,它看起來像一個真正的開關)

知道怎麼讓它在一次點擊中工作?


const body = document.getElementById('body');


let currentBodyClass = body.className;


const darkModeSwitch = document.getElementById('darkModeSwitch');



//Dark Mode


function darkMode() {


 darkModeSwitch.addEventListener('click', () => {


 if (currentBodyClass ==="lightMode") {


 body.className = currentBodyClass ="darkMode";


 } else if (currentBodyClass ==="darkMode") {


 body.className = currentBodyClass ="lightMode";


 }


 });


}

#darkModeSwitch {


 position: absolute;


 left: 15px;


 top: 15px;


 }



.darkMode { background-color: black; transition: ease .3s; }


.lightMode { background-color: #FFF; transition: ease .3s; }



#darkModeSwitch input[type="checkbox"] {


 width: 40px;


 height: 20px;


 background: #fff89d;


}



#darkModeSwitch input:checked[type="checkbox"] {


 background: #757575;


}



#darkModeSwitch input[type="checkbox"]:before {


 width: 20px;


 height: 20px;


 background: #fff;


}



#darkModeSwitch input:checked[type="checkbox"]:before {


 background: #000;


}

<body id="body" class="lightMode">



 <div id="darkModeSwitch">


 <input type="checkbox" onclick="darkMode()" title="Toggle Light/Dark Mode" />


 </div>



</body>

时间:

我在構建一個暗模式開關的應用程序,它在第一次點擊時起作用,但在那之後,它每次點擊都起作用。

這段代碼顯示了一個複選框。在項目中,它看起來像一個真正的開關)

知道怎麼讓它在一次點擊中工作?


const body = document.getElementById('body');


let currentBodyClass = body.className;


const darkModeSwitch = document.getElementById('darkModeSwitch');



//Dark Mode


function darkMode() {


 darkModeSwitch.addEventListener('click', () => {


 if (currentBodyClass ==="lightMode") {


 body.className = currentBodyClass ="darkMode";


 } else if (currentBodyClass ==="darkMode") {


 body.className = currentBodyClass ="lightMode";


 }


 });


}

#darkModeSwitch {


 position: absolute;


 left: 15px;


 top: 15px;


 }



.darkMode { background-color: black; transition: ease .3s; }


.lightMode { background-color: #FFF; transition: ease .3s; }



#darkModeSwitch input[type="checkbox"] {


 width: 40px;


 height: 20px;


 background: #fff89d;


}



#darkModeSwitch input:checked[type="checkbox"] {


 background: #757575;


}



#darkModeSwitch input[type="checkbox"]:before {


 width: 20px;


 height: 20px;


 background: #fff;


}



#darkModeSwitch input:checked[type="checkbox"]:before {


 background: #000;


}

<body id="body" class="lightMode">



 <div id="darkModeSwitch">


 <input type="checkbox" onclick="darkMode()" title="Toggle Light/Dark Mode" />


 </div>



</body>

我在構建一個暗模式開關的應用程序,它在第一次點擊時起作用,但在那之後,它每次點擊都起作用。

這段代碼顯示了一個複選框。在項目中,它看起來像一個真正的開關)

知道怎麼讓它在一次點擊中工作?


const body = document.getElementById('body');


let currentBodyClass = body.className;


const darkModeSwitch = document.getElementById('darkModeSwitch');



//Dark Mode


function darkMode() {


 darkModeSwitch.addEventListener('click', () => {


 if (currentBodyClass ==="lightMode") {


 body.className = currentBodyClass ="darkMode";


 } else if (currentBodyClass ==="darkMode") {


 body.className = currentBodyClass ="lightMode";


 }


 });


}

#darkModeSwitch {


 position: absolute;


 left: 15px;


 top: 15px;


 }



.darkMode { background-color: black; transition: ease .3s; }


.lightMode { background-color: #FFF; transition: ease .3s; }



#darkModeSwitch input[type="checkbox"] {


 width: 40px;


 height: 20px;


 background: #fff89d;


}



#darkModeSwitch input:checked[type="checkbox"] {


 background: #757575;


}



#darkModeSwitch input[type="checkbox"]:before {


 width: 20px;


 height: 20px;


 background: #fff;


}



#darkModeSwitch input:checked[type="checkbox"]:before {


 background: #000;


}

<body id="body" class="lightMode">



 <div id="darkModeSwitch">


 <input type="checkbox" onclick="darkMode()" title="Toggle Light/Dark Mode" />


 </div>



</body>

我在構建一個暗模式開關的應用程序,它在第一次點擊時起作用,但在那之後,它每次點擊都起作用。

這段代碼顯示了一個複選框。在項目中,它看起來像一個真正的開關)

知道怎麼讓它在一次點擊中工作?


const body = document.getElementById('body');


let currentBodyClass = body.className;


const darkModeSwitch = document.getElementById('darkModeSwitch');



//Dark Mode


function darkMode() {


 darkModeSwitch.addEventListener('click', () => {


 if (currentBodyClass ==="lightMode") {


 body.className = currentBodyClass ="darkMode";


 } else if (currentBodyClass ==="darkMode") {


 body.className = currentBodyClass ="lightMode";


 }


 });


}

#darkModeSwitch {


 position: absolute;


 left: 15px;


 top: 15px;


 }



.darkMode { background-color: black; transition: ease .3s; }


.lightMode { background-color: #FFF; transition: ease .3s; }



#darkModeSwitch input[type="checkbox"] {


 width: 40px;


 height: 20px;


 background: #fff89d;


}



#darkModeSwitch input:checked[type="checkbox"] {


 background: #757575;


}



#darkModeSwitch input[type="checkbox"]:before {


 width: 20px;


 height: 20px;


 background: #fff;


}



#darkModeSwitch input:checked[type="checkbox"]:before {


 background: #000;


}

<body id="body" class="lightMode">



 <div id="darkModeSwitch">


 <input type="checkbox" onclick="darkMode()" title="Toggle Light/Dark Mode" />


 </div>



</body>

我在構建一個暗模式開關的應用程序,它在第一次點擊時起作用,但在那之後,它每次點擊都起作用。

這段代碼顯示了一個複選框。在項目中,它看起來像一個真正的開關)

知道怎麼讓它在一次點擊中工作?


const body = document.getElementById('body');


let currentBodyClass = body.className;


const darkModeSwitch = document.getElementById('darkModeSwitch');



//Dark Mode


function darkMode() {


 darkModeSwitch.addEventListener('click', () => {


 if (currentBodyClass ==="lightMode") {


 body.className = currentBodyClass ="darkMode";


 } else if (currentBodyClass ==="darkMode") {


 body.className = currentBodyClass ="lightMode";


 }


 });


}

#darkModeSwitch {


 position: absolute;


 left: 15px;


 top: 15px;


 }



.darkMode { background-color: black; transition: ease .3s; }


.lightMode { background-color: #FFF; transition: ease .3s; }



#darkModeSwitch input[type="checkbox"] {


 width: 40px;


 height: 20px;


 background: #fff89d;


}



#darkModeSwitch input:checked[type="checkbox"] {


 background: #757575;


}



#darkModeSwitch input[type="checkbox"]:before {


 width: 20px;


 height: 20px;


 background: #fff;


}



#darkModeSwitch input:checked[type="checkbox"]:before {


 background: #000;


}

<body id="body" class="lightMode">



 <div id="darkModeSwitch">


 <input type="checkbox" onclick="darkMode()" title="Toggle Light/Dark Mode" />


 </div>



</body>

我在構建一個暗模式開關的應用程序,它在第一次點擊時起作用,但在那之後,它每次點擊都起作用。

這段代碼顯示了一個複選框。在項目中,它看起來像一個真正的開關)

知道怎麼讓它在一次點擊中工作?


const body = document.getElementById('body');


let currentBodyClass = body.className;


const darkModeSwitch = document.getElementById('darkModeSwitch');



//Dark Mode


function darkMode() {


 darkModeSwitch.addEventListener('click', () => {


 if (currentBodyClass ==="lightMode") {


 body.className = currentBodyClass ="darkMode";


 } else if (currentBodyClass ==="darkMode") {


 body.className = currentBodyClass ="lightMode";


 }


 });


}

#darkModeSwitch {


 position: absolute;


 left: 15px;


 top: 15px;


 }



.darkMode { background-color: black; transition: ease .3s; }


.lightMode { background-color: #FFF; transition: ease .3s; }



#darkModeSwitch input[type="checkbox"] {


 width: 40px;


 height: 20px;


 background: #fff89d;


}



#darkModeSwitch input:checked[type="checkbox"] {


 background: #757575;


}



#darkModeSwitch input[type="checkbox"]:before {


 width: 20px;


 height: 20px;


 background: #fff;


}



#darkModeSwitch input:checked[type="checkbox"]:before {


 background: #000;


}

<body id="body" class="lightMode">



 <div id="darkModeSwitch">


 <input type="checkbox" onclick="darkMode()" title="Toggle Light/Dark Mode" />


 </div>



</body>

我在構建一個暗模式開關的應用程序,它在第一次點擊時起作用,但在那之後,它每次點擊都起作用。

這段代碼顯示了一個複選框。在項目中,它看起來像一個真正的開關)

知道怎麼讓它在一次點擊中工作?


const body = document.getElementById('body');


let currentBodyClass = body.className;


const darkModeSwitch = document.getElementById('darkModeSwitch');



//Dark Mode


function darkMode() {


 darkModeSwitch.addEventListener('click', () => {


 if (currentBodyClass ==="lightMode") {


 body.className = currentBodyClass ="darkMode";


 } else if (currentBodyClass ==="darkMode") {


 body.className = currentBodyClass ="lightMode";


 }


 });


}

#darkModeSwitch {


 position: absolute;


 left: 15px;


 top: 15px;


 }



.darkMode { background-color: black; transition: ease .3s; }


.lightMode { background-color: #FFF; transition: ease .3s; }



#darkModeSwitch input[type="checkbox"] {


 width: 40px;


 height: 20px;


 background: #fff89d;


}



#darkModeSwitch input:checked[type="checkbox"] {


 background: #757575;


}



#darkModeSwitch input[type="checkbox"]:before {


 width: 20px;


 height: 20px;


 background: #fff;


}



#darkModeSwitch input:checked[type="checkbox"]:before {


 background: #000;


}

<body id="body" class="lightMode">



 <div id="darkModeSwitch">


 <input type="checkbox" onclick="darkMode()" title="Toggle Light/Dark Mode" />


 </div>



</body>

我在構建一個暗模式開關的應用程序,它在第一次點擊時起作用,但在那之後,它每次點擊都起作用。

這段代碼顯示了一個複選框。在項目中,它看起來像一個真正的開關)

知道怎麼讓它在一次點擊中工作?


const body = document.getElementById('body');


let currentBodyClass = body.className;


const darkModeSwitch = document.getElementById('darkModeSwitch');



//Dark Mode


function darkMode() {


 darkModeSwitch.addEventListener('click', () => {


 if (currentBodyClass ==="lightMode") {


 body.className = currentBodyClass ="darkMode";


 } else if (currentBodyClass ==="darkMode") {


 body.className = currentBodyClass ="lightMode";


 }


 });


}

#darkModeSwitch {


 position: absolute;


 left: 15px;


 top: 15px;


 }



.darkMode { background-color: black; transition: ease .3s; }


.lightMode { background-color: #FFF; transition: ease .3s; }



#darkModeSwitch input[type="checkbox"] {


 width: 40px;


 height: 20px;


 background: #fff89d;


}



#darkModeSwitch input:checked[type="checkbox"] {


 background: #757575;


}



#darkModeSwitch input[type="checkbox"]:before {


 width: 20px;


 height: 20px;


 background: #fff;


}



#darkModeSwitch input:checked[type="checkbox"]:before {


 background: #000;


}

<body id="body" class="lightMode">



 <div id="darkModeSwitch">


 <input type="checkbox" onclick="darkMode()" title="Toggle Light/Dark Mode" />


 </div>



</body>

...