Get the App
SLTechnology News&Howtos  ›  Development  › 

How to change the background color of a web page by JavaScript

Shulou Source: shulou.com Published: 2022-06-02 01:42:38 10月02日 Update

JavaScript how to change the background color of the page, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

JavaScript changes the background color of a web page

Example 1: enter a color name to change the background color of a web page

Enter the name of the color in a text box (e.g. blue, purple) and click the button next to the text box to change the background color of the page to the color corresponding to the name in the text box.

Implementation code

Web discoloration function show () {var x=document.getElementsByTagName ("body"); var y=document.getElementById ("i1"); var c1=document.getElementById ("i2") .value; var c2 Switch (C1) {case 'blue': c2 = "blue"; break; case 'yellow': c2 = "yellow"; break; case 'light blue': c2 = "lightblue"; break; case 'purple': c2 = "purple"; break Case 'pink': c2 = "pink"; break;} y.style.backgroundColorcolors C2;}

Tags: Web page background color element code variable name button text example purple result blue language help input clear beautiful between key Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi vpn MySQL MariaDB