abstrakt:<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>獲取隨機(jī)顏色</title> <script type="text/javascript" src="jq
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>獲取隨機(jī)顏色</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:數(shù)學(xué)函數(shù)對(duì)象 floor 作用:四舍五入 random:隨機(jī) 作用:獲取隨機(jī)值 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>
為什么只有第一個(gè)a標(biāo)簽添加了隨機(jī)數(shù),不懂怎么解決
Korrigierender Lehrer:韋小寶Korrekturzeit:2019-01-21 11:59:39
Zusammenfassung des Lehrers:其他的標(biāo)簽頁(yè)可以加啊 你把id才能class id是唯一的啊 你要使用class接著再換一下class選擇器就可以了啊