JQ随机颜色作业

原创 2019-01-11 09:30:04 321
摘要:函数里用的原生的JS写的 获取随机颜色,用document.getElementsByTagName可以获取到数组一再来for 循环,问题:假如 直接 $('a'),这样能否和上面一样获取到元素的数组呢?关于JQ获取标签数组的方式是怎么样的?<html><head><meta charset="utf-8&quo

函数里用的原生的JS写的 获取随机颜色,用document.getElementsByTagName可以获取到数组一再来for 循环,
问题:假如 直接 $('a'),这样能否和上面一样获取到元素的数组呢?关于JQ获取标签数组的方式是怎么样的?

<html>
<head>
<meta charset="utf-8">
<title>JQ随机颜色作业</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>


批改老师:查无此人批改时间:2019-01-11 09:33:54
老师总结:有问题尽量在工单和群里问,作业里问了,我回答你后,你无法在追问。 完成的不错,JQ有循环,后面的章节应该可以看到视频。

发布手记

热门词条