更改所有CSS项目的Javascript函数

是否有任何 javascript 函数可以在以下代码中将所有给定的 CSS 样式更改为其他 CSS 样式。看,我希望给定链接中的body.container1.button1和所有其他内容变成body.darkcontainer1.dark等等。

https://codepen.io/vkdatta27/pen/poyYapw



九州编程
浏览 103回答 2
2回答

繁华开满天机

为什么不调整你的 css,这样你只改变一个?保持卫生。function toggleDark() {&nbsp; const body = document.querySelector('body');&nbsp; if (body.classList.contains('dark')) {&nbsp; &nbsp; body.classList.remove('dark');&nbsp; } else {&nbsp; &nbsp; body.classList.add('dark');&nbsp; }}document.querySelector('#darkmode').addEventListener('click', toggleDark);body {&nbsp; background-color: #c0c0c0;}body.dark {&nbsp; background-color: #272727;}.container1 {&nbsp; width: fit;&nbsp; background-color: lightblue;&nbsp; height: fit;&nbsp; color: green;}body.dark .container1 {&nbsp; width: fit;&nbsp; background-color: yellow;&nbsp; height: fit;&nbsp; color: red;}.container2 {&nbsp; width: fit;&nbsp; background-color: black;&nbsp; height: fit;&nbsp; color: white;}body.dark .container2 {&nbsp; width: fit;&nbsp; background-color: white;&nbsp; height: fit;&nbsp; color: black;}.button1 {&nbsp; background-color: #c0c0c0;&nbsp; color: black;&nbsp; border-radius: 25px;&nbsp; border: 1px solid black}body.dark .button1 {&nbsp; background-color: #white;&nbsp; color: black;&nbsp; border-radius: 25px;&nbsp; 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&nbsp; 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&nbsp; 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>

跃然一笑

选择按钮并监听事件click。每当您单击一个按钮时,您都会想要调用一个函数,该函数循环遍历您指定的元素并用于向这些元素classList.toggle('dark')添加或删除.dark类。const button = document.querySelector('#darkmode');const elements = document.querySelectorAll('body, .container1, .button1');button.addEventListener('click', () => {&nbsp; for (const element of elements) {&nbsp; &nbsp; element.classList.toggle('dark');&nbsp; }});但是使用 CSS 的力量可能更容易。像这样:.container1 {&nbsp; background-color: lightblue;}.dark .container1 {&nbsp; background-color: yellow;}现在,您只需dark在所有元素的父元素(例如 )上添加或删除类,body即可打开或关闭深色模式。const button = document.querySelector('#darkmode');button.addEventListener('click', () => {&nbsp; document.body.classList.toggle('dark');});哦,小旁注:fit不是该属性的有效值width。检查MDN以获取有效值。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript