摘要:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>获取随机颜色/数字</title> <script type="text/javascript" src="
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>获取随机颜色/数字</title> <script type="text/javascript" src="jquery-3.3.1.min.js"></script> <style type="text/css"> div{width:200px;height:200px;border:1px solid #ccc;margin-bottom:10px;padding-left:30px;} </style> </head> <body> <script type="text/javascript"> function bb(did){ var len=document.getElementsByTagName(did).length//获取元素长度 for(var i=0;i<len;i++){ document.getElementsByTagName(did)[i].style.backgroundColor='rgb('+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+')'//获取随机rgb颜色 } } $(document).ready(function(){ $('#but1').click(function(){ bb('div') var sjs=Math.floor(Math.random()*100)//获取100内的随机整数 $('div').text('随机数字为:'+sjs) //更改随机数字 var yuanjiao=Math.floor(Math.random()*25)//获取25以内的随机整数 $('div').css('borderRadius',yuanjiao+'px')//更改随机圆角 }) }) </script> <div id="aa">随机数字为:</div> <button id="but1">变色</button> </body> </html>
批改老师:韦小宝批改时间:2019-01-20 12:55:22
老师总结:写的很不错 这种小案例没事的时候多找点练习练习