abstract:<!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>
Correcting teacher:韋小寶Correction time:2019-01-20 12:55:22
Teacher's summary:寫的很不錯 這種小案例沒事的時候多找點練習練習