Home >Web Front-end >JS Tutorial >JavaScript method to switch the background color of a web page by clicking a button_javascript skills

JavaScript method to switch the background color of a web page by clicking a button_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:36:142572browse

The example in this article describes how to use JavaScript to switch the background color of a web page by clicking a button. Share it with everyone for your reference, the details are as follows:

Here is a demonstration of JavaScript using buttons to change the background color at will. Every time you click the button, you can randomly change the background color of a web page. The color values ​​are stored in JS arrays in advance. You will find them in the code. If you don't want a certain color, just replace it.

The screenshot of the running effect is as follows:

The online demo address is as follows:

http://demo.jb51.net/js/2015/js-btn-click-rand-bgcolor-codes/

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>通过按钮变换背景颜色</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="变换背景" onclick="turncolors()">
</p>
 <p>用按钮变换背景颜色.</p>
</form>
</body>
</html>

I hope this article will be helpful to everyone in JavaScript programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn