随机改变颜色

这个案例实现了点击button的时候, 出发getcolor函数,随机返回一个颜色,将这个颜色赋值给div的background,就实现了点击button,随即改变颜色的效果,

该案例主要使用了Math对象中的random方法;

源代码如下,需引入jquery.js

 

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>removeAttr demo</title>
<script src="jquery.js"></script>
<style type="text/css">
div{
width:200px;
height:200px;
background-color:red;
}
</style>
</head>
<body>
<button>点击随机改变下方矩形颜色</button>
<div></div>
</body>
</html>
<script>
$("button").click(function(){
$("div").css("background-color",getcolor())
})
function getcolor(){
var rgb=[];
rgb[0]=Math.floor(Math.random()*255);
rgb[1]=Math.floor(Math.random()*255);
rgb[2]=Math.floor(Math.random()*255);
var abc = "rgb" + "("+rgb[0]+","+rgb[1]+","+rgb[2]+")";
return abc
}
</script>

posted @ 2017-03-20 10:19  boboweiqi93  阅读(793)  评论(0编辑  收藏  举报