Requirements: Click on the page button to change the color of the page
Idea: First draw the simplest page, two ways to get the body node of the page, three ways to modify the body node background color properties, four through a method to get random color values
Compared to the second example (js-changing the color of the page (ii)), the idea of creating a color value is only changed
The simple code snippet is as follows:
<!DOCTYPE HTML><HTML><Head> <MetaCharSet= "Utf-8"/> <title>Change Page Color Script</title> <Script> /*change the background color of a page*/ functionChangepagecolor () {//get the BODY element . varBodyarray=document.getElementsByTagName ("Body"); varBody=bodyarray[0]; //Modify the background color property of the BODY elementBody.setattribute ("bgcolor", Getcolorvalue ()); } /*get the hexadecimal representation value of the background color, this method is also the previous paragraph simple verification Code writing idea*/ functionGetcolorvalue () {//This is no longer a random color, you can choose several colors according to your preference, as an alternative page to change the color varChararray= NewArray ('AliceBlue', 'Chartreuse', 'Darkgreen', 'Greenyellow', 'Limegreen', 'Palegreen', 'Springgreen', 'Red', 'Orange', 'Mediumspringgreen','Indigo', 'Darkmagenta', 'Blue', 'Darkgray', 'Fuchsia', 'Lime'); varCharIndex=Math.floor (Math.random ()* -); returnChararray[charindex]; } </Script></Head><Bodybgcolor= "Green"Align= "Center"> <inputtype= "button"value= "Changepagecolor"onclick= "Changepagecolor ();"/></Body></HTML>
js-changing the color of the page (iii)