How do I change the background color with JavaScript?

后端 未结 19 2650
春和景丽
春和景丽 2020-11-22 13:59

Anyone know a simple method to swap the background color of a webpage using JavaScript?

19条回答
  •  感动是毒
    2020-11-22 14:55

    Modify the JavaScript property document.body.style.background.

    For example:

    function changeBackground(color) {
       document.body.style.background = color;
    }
    
    window.addEventListener("load",function() { changeBackground('red') });
    

    Note: this does depend a bit on how your page is put together, for example if you're using a DIV container with a different background colour you will need to modify the background colour of that instead of the document body.

提交回复
热议问题