简体   繁体   中英

How do you change the background color of your HTML document using JavaScript

I've tried many different examples, but none seem to work. :(. Please find the code snippet below.

var element = document.getElementById("time");
element.style.backgroundColor = "#ff0000";

The code above didn't work for me and I have no idea why.

Please help!

You can get the HTML tag with document.documentElement to apply styles to.

 document.documentElement.style.backgroundColor = "#ff0000";

You can also get the body tag with document.body to apply styles to.

 document.body.style.backgroundColor = "#ff0000";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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