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 first example (js-changing the color of the page (a)), the idea of generating 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 (6)); }/*get the hexadecimal representation value of the background color, this method is also the previous paragraph simple verification Code writing idea*/ functionGetcolorvalue (len) {varColorValue= "#"; varChararray= NewArray (0, 1, 2, 3, 4, 5, 6, 7, 8, 9,'A', 'B', 'C', 'D', 'E', 'F'); for (varI= 0; I<Len; I++) { varCharIndex=Math.floor (Math.random ()* -); ColorValue+=Chararray[charindex]; } returnColorValue; } </Script></Head><Bodybgcolor= "Green"Align= "Center"> <inputtype= "button"value= "Changepagecolor"onclick= "Changepagecolor ();"/></Body></HTML>
js-change the color of the page (ii)