模仿案例
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>获取随机颜色</title> <script type="text/javascript" src="jquery-3.3.1.min.js"> </script> <style> a{ float: left; display: inline-block; margin:50px; width: 100px; height: 100px; line-height: 100px; text-align: center; text-decoration: none; color: pink; border-radius: 20px; } </style> <script type="text/javascript"> function getcoler(tag){ var length=document.getElementsByTagName(tag).length//获取a标签的个数 for (var i = 0; i<length; i++) { document.getElementsByTagName(tag)[i].style.backgroundColor='rgb('+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+')' //为每个a标签获取随机颜色值,颜色值用rgb('')表示,具体数值值用随机函数Math.random()和取整函数获得Math.floor() } } $(document).ready(function(){ getcoler('a') $('a').mouseover(function(){ a_rgb=$(this).css('backgroundColor')//获取a标签的背景颜色 $(this).css('box-shadow','0px 0px 20px '+a_rgb) //为当前a标签的加上同颜色的阴影 $(this)括号里不用加'' //注意'0px 0px 20px '每个值后都要有空格,包括20px后面 $(this).css('border-radius','50px') }), $('a').mouseleave(function(){ $(this).css('box-shadow','none') //为当前a标签的去掉的阴影 $(this).css('border-radius','20px') }) }) </script> </head> <body> <a href="#">1</a> <a href="#">2</a> <a href="#">3</a> <a href="#">4</a> </body> </html>