"JavaScript" produces random colors

Source: Internet
Author: User

If you want to do the following, each time you refresh the page produces a color,

In fact, it's very simple, the fundamental core of the random color is the question of a six-digit, JavaScript random number that is created with the mechanism.

And this six-digit number is within each digit 0~f, so there is the following method:

1, the first is an HTML layout, p tag is used to put the current color, div background color is this color

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >
2, followed by the core script:

<script>//color string var colorstr= "";//the range of each character of the string var randomarr=[' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ' , ' C ', ' d ', ' e ', ' F '];//produces a six-bit string for (Var i=0;i<6;i++) {//15 is the range cap, 0 is the lower range, and two functions guarantee that the resulting random number is an integer colorstr+=randomarr[ Math.ceil (Math.random () * (15-0) +0)]; document.getElementById ("Colorstr"). innerhtml= "COLOR: #" +colorstr;document.getelementbyid ("Div1"). Style.backgroundcolor= "#" +colorstr;</script>



"JavaScript" produces random colors

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.