Home>

Here demonstrates the use of buttons to change the background color at will,With each click of the button,You can randomly change the background color of a web page.The color values ​​are stored in the js array beforehand.You will find them in the code,If you do n’t want a color,Just replace it.

The screenshot of the running effect is as follows:

The online demo address is as follows:

The specific code is as follows:

<! Doctype html public "-// w3c // dtd html 4.01 transitional // en"
"http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<title>Change background color by button</title>
</head>
<body>
<script language="javascript">
var arraycolor=new array ("olive", "teal", "red", "blue", "maroon", "navy", "lime", "fuschia", "green", "purple", "gray", "yellow", "aqua", "white", "silver");
var n=0;
function turncolors () {
  if (n == (arraycolor.length-1)) n=0;
  n ++;
  document.bgcolor=arraycolor [n];
}
</script>
<form name="form1" method="post" action="">
 <p>
 <input type="button" name="submit" value="transform background" onclick="turncolors ()">
</p>
 <p>Transform background color with buttons.
</p>
</form>
</body>
</html>
  • Previous If statement for batch command teaching
  • Next JavaScript to determine character length, numbers, email, phone and other commonly used judgment functions