Blogger Information
Blog 20
fans 0
comment 0
visits 25109
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js实现生成随机色
左手Leon的博客
Original
616 people have browsed it

实例

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>随机颜色</title>
	<style type="text/css">
		*{margin:0;padding: 0}
		div{
			width:1000px;
			height:800px;
			margin:50px auto;
		}
	</style>
	<script type="text/javascript" src="static/js/jquery.js"></script>
</head>
<body>
	<div class="box">
		<button onclick="bg_color()">生成随机背景颜色</button>
	</div>
	<script>
		function bg_color(){
			var bg='#';
			r=Math.floor(Math.random()*16).toString(16)+Math.floor(Math.random()*16).toString(16);
			g=Math.floor(Math.random()*16).toString(16)+Math.floor(Math.random()*16).toString(16);
			b=Math.floor(Math.random()*16).toString(16)+Math.floor(Math.random()*16).toString(16);
			bg+=r+g+b;
			console.log(bg);
			document.getElementsByClassName('box')[0].style.background=bg;
			
		}
	</script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

心得:

toString()要生成十六进制时,要在里面加参数16

另有通过做好一个包含十六进制数的数组,生成随机的数组下标,循环组合出颜色值

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!