js-change the color of the page (ii)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.