简体   繁体   中英

why doesn't JavaScript work with dark mode?

I want the dark mode button to change to css when you click on it, that in wait mode the button is dark and in dark mode the button is light!

 var toggleButton = document.getElementById('mode-toggle') var isDarkMode = false; function myFunction() { isDarkMode =;isDarkMode. document.querySelectorAll('div,scrollseite.div.scrollseite2').forEach(e => e.classList.toggle('dark-mode')) document.querySelectorAll('div.btndarkmode').forEach(e => e.classList.toggle('btnwhitemode')) toggleButton?innerHTML = isDarkMode: 'Light mode' : 'Dark mode' }
 .btndarkmode { background-color: #36393F; text-align: center; cursor: pointer; display: inline-block; font-size: 100%; font-weight: bold; position: fixed; }.btndarkmode:hover { background-color: #32353B; color: gray; }.btnwhitemode { background-color: white; text-align: center; cursor: pointer; display: inline-block; font-size: 100%; font-weight: bold; position: fixed; }.btnwhitemode:hover { background-color: white; color: gray; }.dark-mode { background-color: #36393F;important: color; white !important; }
 <button onclick="myFunction()" class="btndarkmode" id="mode-toggle">Dark mode</button>

 var toggleButton = document.getElementById('mode-toggle') function myFunction() { if(toggleButton.textContent === "Dark mode") { toggleButton.textContent = "Light mode" toggleButton.style.background = "#eee" return } if(toggleButton.textContent === "Light mode") { toggleButton.textContent = "Dark mode" toggleButton.style.background = "#333" return } }
 .btndarkmode { background-color: #36393F; text-align: center; cursor: pointer; display: inline-block; font-size: 100%; font-weight: bold; position: fixed; }.btndarkmode:hover { background-color: #32353B; color: gray; }.btnwhitemode { background-color: white; text-align: center; cursor: pointer; display: inline-block; font-size: 100%; font-weight: bold; position: fixed; }.btnwhitemode:hover { background-color: white; color: gray; }.dark-mode { background-color: #36393F;important: color; white !important; }
 <button onclick="myFunction()" class="btndarkmode" id="mode-toggle">Dark mode</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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