????:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>獲取隨機顏色/數(shù)字</title> <script type="text/javascript" src="
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>獲取隨機顏色/數(shù)字</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內(nèi)的隨機整數(shù) $('div').text('隨機數(shù)字為:'+sjs) //更改隨機數(shù)字 var yuanjiao=Math.floor(Math.random()*25)//獲取25以內(nèi)的隨機整數(shù) $('div').css('borderRadius',yuanjiao+'px')//更改隨機圓角 }) }) </script> <div id="aa">隨機數(shù)字為:</div> <button id="but1">變色</button> </body> </html>
?? ???:韋小寶?? ??:2019-01-20 12:55:22
???? ??:寫的很不錯 這種小案例沒事的時候多找點練習(xí)練習(xí)