使用 JavaScript 动态更改背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17312867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:50:46  来源:igfitidea点击:

Change background color dynamically with JavaScript

javascripthtmlcss

提问by Jethro Ncis

In my css a background-color is defined set as white, but I want to change color dynamically body of page.

在我的 css 中,背景颜色被定义为白色,但我想动态更改页面正文的颜色。

How do you do that? my CSS file:

你是怎样做的?我的 CSS 文件:

body {
    color: #333;
    background-color: #FFF;
}

my javascript code :

我的 javascript 代码:

function init() 
{
    document.body.style.zoom="90%";
    document.body.color="white";
}

Thank you.

谢谢你。

回答by Arun P Johny

For changing the style you need to access the styleproperty, and the property of background-coloris backgroundColor

要更改样式,您需要访问样式属性,其属性background-colorbackgroundColor

 document.body.style.backgroundColor="white"