Home >Web Front-end >JS Tutorial >How can I change the background color of a webpage using JavaScript?
Altering the Background Color with JavaScript
Changing the background color of a webpage with JavaScript is a straightforward task. Here's how to do it:
Solution:
Here's a sample function to achieve this:
function changeBackground(color) { document.body.style.background = color; }
Toinvoke the function, add the following code to the page's onload event handler:
window.addEventListener("load",function() { changeBackground('red') });
Note:
Consider the page's structure. If the background is set to a specific DIV container, you need to modify its background color instead of the document body.
The above is the detailed content of How can I change the background color of a webpage using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!