刷新随机颜色小案例

原创2019-01-21 11:39:4286
摘要:<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>获取随机颜色</title> <script type="text/javascript" src="jq
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>获取随机颜色</title>
	<script type="text/javascript" src="jquery-3.3.1.js"></script>
	<style type="text/css">
		a{
			width: 100px;
			height: 100px;
			display: block;
			float: left;
			margin: 20px;
			border-radius: 50px;
			line-height:100px;
			text-align: center;
			text-decoration:none;
		}
		.clear{clear: both}
	</style>
	<script type="text/javascript">
		function aa(tag1) {
			var len=document.getElementsByTagName(tag1).length
			for(var i=0;i<len;i++){
				//Math:数学函数对象   floor 作用:四舍五入     random:随机  作用:获取随机值
				document.getElementsByTagName(tag1)[i].style.backgroundColor='rgb('+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+')'
				document.getElementById('num').innerHTML=Math.floor(Math.random()*1000)
		    }
	    }
		$(document).ready(function(){
			$('button').click(function(){
				aa('a')
				
			})
		})
	</script>
</head>
<body>
	<a href="" id="num"></a>
	<a href="" id="num"></a>
	<a href="" id="num"></a>
	<a href="" id="num"></a>
    <div class="clear"></div>
	<button>刷新</button>
</body>	
</html>

为什么只有第一个a标签添加了随机数,不懂怎么解决

批改老师:韦小宝批改时间:2019-01-21 11:59:39
老师总结:其他的标签页可以加啊 你把id才能class id是唯一的啊 你要使用class接着再换一下class选择器就可以了啊

发布手记

热门词条