简体   繁体   English

我希望在单击按钮时更改页面的 CSS

[英]I want the CSS of a page to change when a button is clicked

I found how you could do this with individual elements but I couldn't find anything that would change the the body CSS of a page.我找到了如何使用单个元素执行此操作,但找不到任何会更改页面正文 CSS 的内容。 I am trying to make a thing where it will switch between light and dark mode when you click the button.我正在尝试制作一个当您单击按钮时它将在明暗模式之间切换的东西。

<!DOCTYPE html>
<button style="background-color: #252525;
border: 2px;
border-style: solid;
border-color: white;
color: white; 
padding: 10px 32px;
text-align: center;
text-decoration: none; 
display: inline-block;
font-size: 16px;  
font-weight: bold;
margin: 4px 2px;
cursor: pointer;" id="changethemebutton" onclick="changeTheme()">Change Theme</button>

function changeTheme() {
document.getElementById(".background").style.background = "black";
}
</script>
<style>
body{
    background-color:white
}
</style>

Best way to set a dark-mode on your website is to toggle a class .dark-mode on your body using javascript and then stylize it using css:在您的网站上设置暗模式的最佳方法是使用 javascript 在您的身体上切换类.dark-mode ,然后使用 css 对其进行.dark-mode化:

 body { /* light-mode styles */ background-color: white; } body.dark-mode { /* dark-mode styles */ background-color: black; }
 <button style="background-color: #252525; border: 2px; border-style: solid; border-color: white; color: white; padding: 10px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; font-weight: bold; margin: 4px 2px; cursor: pointer;" id="changethemebutton" onclick="changeTheme()">Change Theme </button> <script> function changeTheme() { document.body.classList.toggle('dark-mode'); // <-- Toogle dark-mode class to <body> } </script>

You're trying to target an element with a class using an incorrect method.您正在尝试使用不正确的方法定位具有类的元素。 getElementById targets elements with a specified id . getElementById以具有指定id元素为目标。 If you're trying to target a class, the most ideal method to do this with is querySelector :如果您尝试定位一个类,最理想的方法是使用querySelector

document.querySelector(".background").style.background = "black";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我点击第二个按钮时,我想更改第一个按钮的颜色 - I want to change the color of the first Button, when I clicked on the second 单击按钮时,使用jQuery更改CSS定义 - Change CSS Definition with jQuery when button is clicked 单击ATBar按钮时更改div的CSS - Change CSS of div when ATBar button is clicked 当单击按钮时,我希望多个选定的复选框行从弹出页面显示到父页面 - when the button is clicked i want the multiple selected checkbox row to be displayed from popup page to parent page 单击按钮时更改页面布局 - Change the Page layout when a button is clicked 想要按钮在页面加载时隐藏内容,然后在单击按钮时显示 - Want button to hide content on page load then display when button clicked 当我单击特定的“addToBasket”按钮时,我不想更改所有“addToBasket”按钮。 只有点击的按钮应该改变 - I do not want all the "addToBasket" buttons to change when i click on a specefic "addToBasket" button. Only the clicked button should change 单击不同 html 页面上的按钮时,如何添加 css 样式更改? - How to add a css style change when a button on different html page is clicked? 我需要使用 HTML、CSS 和 Javascript 制作一个按钮,以便在单击按钮时将字体颜色从红色变为红色 - I need to make a button using HTML, CSS, and Javascript to change font color from to red when the button is clicked 要在移动设备上单击“后退”按钮时转到主页? - want to go to home page when clicked on back button in mobile?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM