繁华开满天机
为什么不调整你的 css,这样你只改变一个?保持卫生。function toggleDark() { const body = document.querySelector('body'); if (body.classList.contains('dark')) { body.classList.remove('dark'); } else { body.classList.add('dark'); }}document.querySelector('#darkmode').addEventListener('click', toggleDark);body { background-color: #c0c0c0;}body.dark { background-color: #272727;}.container1 { width: fit; background-color: lightblue; height: fit; color: green;}body.dark .container1 { width: fit; background-color: yellow; height: fit; color: red;}.container2 { width: fit; background-color: black; height: fit; color: white;}body.dark .container2 { width: fit; background-color: white; height: fit; color: black;}.button1 { background-color: #c0c0c0; color: black; border-radius: 25px; border: 1px solid black}body.dark .button1 { background-color: #white; color: black; border-radius: 25px; border: 1px solid black}<div class="container1">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div><br /><div class="container2">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div><br /><button id="darkmode" class="button1">🌕 Lights Off!!</button>