【问题标题】:How to change the color of all text on webpage using JS?如何使用 JS 更改网页上所有文本的颜色?
【发布时间】:2020-03-28 13:24:44
【问题描述】:
我正在尝试在我的网站上制作一个按钮来更改背景和文本/字体颜色。
这就是我现在拥有的:
some text<br>
<input type="button" value="Dark" onClick="document.body.style.backgroundColor = 'black'; document.body.style.color = 'white';">
我要更改背景颜色,但我似乎无法更改文本/字体颜色,有人可以帮忙吗?
【问题讨论】:
标签:
javascript
html
css
text
colors
【解决方案1】:
我为按钮添加了一个id,并做了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';">
这会将按钮的文本更改为白色。
JSFiddle - https://jsfiddle.net/yns15r6k/
【解决方案2】:
创建一个函数来处理点击。让我们称之为 handleClick 并将更改颜色和背景颜色的语句放入其中。然后 onClick={this.handleClick}。