摘要:作业有两点要求:点击按钮刷新颜色;在a链接中加入数字,每次点击都为随机值。 第一个要求只需在文档就绪函数中加入点击事件,使点击a标签的时候会有随机颜色生成。 第二个要求中,我设置的事点击a标签时生成1到100之间的随机数,也是在点击事件中。 这两个要求的代码如下
作业有两点要求:
点击按钮刷新颜色;
在a链接中加入数字,每次点击都为随机值。
第一个要求只需在文档就绪函数中加入点击事件,使点击a标签的时候会有随机颜色生成。
第二个要求中,我设置的事点击a标签时生成1到100之间的随机数,也是在点击事件中。
这两个要求的代码如下:
$('a').click(function(){ $(this).css('background','rgb('+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+ ','+Math.floor(Math.random()*256)+')') $(this).text(Math.floor(Math.random()*100)) })
完整代码如下:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>获取随机色</title> <script type="text/javascript" src="jquery-3.3.1.js"></script> <style type="text/css"> a { float:left; display:block; margin:50px; width:100px; line-height: 100px; text-align: center; height:100px; color:#fff; border-radius: 50px; text-decoration: none; } </style> <script type="text/javascript"> //改变标签的背景颜色 function aa(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).ready(function(){ aa('a') $('a').mouseover(function(){ $bg=$(this).css('backgroundColor') $(this).css('box-shadow','0px 0px 20px '+$bg) $(this).css('border-radius','20px ') }) $('a').mouseleave(function(){ $(this).css('box-shadow','none') $(this).css('border-radius','50px') }) $('a').click(function(){ $(this).css('background','rgb('+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+')') $(this).text(Math.floor(Math.random()*100)) }) }) </script> </head> <body> <a href="#"></a> <a href="#"></a> <a href="#"></a> <a href="#"></a> </body> </html>
小结:其实做完了之后才发现添加的作业要求并不是很复杂,但是一开始的时候卡了壳想了很久没想出来,后来思路通了几分钟就把需要添加的内容完成了。是在每次点击a标签的时候生成随机值中不知道怎么把值放到生成的随机色中,可能的原因是过年这几天没怎么看视频比较生疏了,之后的学习中不光要学习新的知识,同时也要及时复习之前学过的东西,这样才会有所进步。
批改老师:韦小宝批改时间:2019-02-20 09:05:35
老师总结:写的很棒 没有任何毛病 写代码会卡壳是很正常的 没事要记得多练习来提高代码量