本文实例讲述了JavaScript实现点击按钮切换网页背景色的方法。分享给大家供大家参考,具体如下:
这里演示JavaScript用按钮随意变换背景颜色,每点击一下按钮,就可以随机改变一种网页背景颜色。颜色值事先是保存在JS的数组里,你在代码里会发现他们,若不想要某个颜色,就替换掉即可。
运行效果截图如下:
在线演示地址如下:
http://demo.jb51.net/js/2015/js-btn-click-rand-bgcolor-codes/
具体代码如下:
<!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>
希望本文所述对大家JavaScript程序设计有所帮助。