简体   繁体   English

如何使用JS更改网页上所有文本的颜色?

[英]How to change the color of all text on webpage using JS?

I am trying to make a button on my website that changes the background and text/font color.我正在尝试在我的网站上制作一个按钮来更改背景和文本/字体颜色。

This is what I have right now:这就是我现在所拥有的:

 some text<br> <input type="button" value="Dark" onClick="document.body.style.backgroundColor = 'black'; document.body.style.color = 'white';">

I got the background color to change but I can't seem to get the text/font color to change, would anyone be able to help?我改变了背景颜色,但我似乎无法改变文本/字体颜色,有人能帮忙吗?

I added an id to the button and did document.getElementById('button').style.color = 'white'我在按钮上添加了一个 id 并做了document.getElementById('button').style.color = 'white'

Full Example: <input type="button" value="Dark" id="button" onClick="document.body.style.backgroundColor = 'black'; document.getElementById('button').style.color = 'white';">完整示例: <input type="button" value="Dark" id="button" onClick="document.body.style.backgroundColor = 'black'; document.getElementById('button').style.color = 'white';">

This changed the text to white for the button.这将按钮的文本更改为白色。

JSFiddle - https://jsfiddle.net/yns15r6k/ JSFiddle - https://jsfiddle.net/yns15r6k/

Create a function to handle the click.创建一个函数来处理点击。 Lets call it handleClick and put the statements for changing the color and background color inside.让我们称它为handleClick 并将更改颜色和背景颜色的语句放在里面。 Then onClick={this.handleClick}.然后 onClick={this.handleClick}。

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

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