abstrait:函數(shù)里用的原生的JS寫的 獲取隨機顏色,用document.getElementsByTagName可以獲取到數(shù)組一再來for 循環(huán),問題:假如 直接 $('a'),這樣能否和上面一樣獲取到元素的數(shù)組呢?關于JQ獲取標簽數(shù)組的方式是怎么樣的?<html><head><meta charset="utf-8&quo
函數(shù)里用的原生的JS寫的 獲取隨機顏色,用document.getElementsByTagName可以獲取到數(shù)組一再來for 循環(huán),
問題:假如 直接 $('a'),這樣能否和上面一樣獲取到元素的數(shù)組呢?關于JQ獲取標簽數(shù)組的方式是怎么樣的?
<html> <head> <meta charset="utf-8"> <title>JQ隨機顏色作業(yè)</title> <script type="text/javascript" src="https://www.huatu.com/images/2015css/js/jquery-1.7.1.min.js"></script> <!--<script src="jquery-3.3.1.min.js"></script> <script src='jquery.color.js'></script>--> </head> <style> a{display:inline-block;width:100px;height:100px;background:#ccc;text-align:center;line-height:60px} </style> <script> var rdNumber = Math.floor(Math.random(0,1)*256); //console.log(rdNumber); function rdBGC(tag){ var len = document.getElementsByTagName(tag).length; for(var i=0;i<=len;i++){ document.getElementsByTagName(tag)[i].style.backgroundColor="rgb("+Math.floor(Math.random()*256)+","+Math.floor(Math.random()*256)+","+Math.floor(Math.random()*256)+")" document.getElementsByTagName(tag)[i].style.borderRadius="50px"; document.getElementsByTagName(tag)[i].innerHTML="<p style='color:#fff;font-weight:bold'>"+Math.floor(Math.random()*1000)+"</p>" } } /*$('document').ready(function(){ rdBGC('a'); })*/ $(function(){ $('#change').on('click',function(){ //$("a").html("<p>"+Math.floor(Math.random()*1000)+"</p>"); rdBGC('a'); }) }) </script> <body> <a></a> <a></a> <a></a> <a></a> <a></a> <button id='change'>點我隨機變化</button> </body> </html>
Professeur correcteur:查無此人Temps de correction:2019-01-11 09:33:54
Résumé du professeur:有問題盡量在工單和群里問,作業(yè)里問了,我回答你后,你無法在追問。 完成的不錯,JQ有循環(huán),后面的章節(jié)應該可以看到視頻。