<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript" src="static/jQuery/jquery-3.3.1.js"></script> <style type="text/css"> a{ display: block; height: 100px; width: 100px; border-radius: 50px; text-align: center; line-height: 100px; color: #fff; float: left; background: #ff9c01; margin: 20px; text-decoration: none; } button{ height: 100px; width: 100px; } </style> <script type="text/javascript"> // 颜色随机切换函数 function changColor(tag) { var tag_len = document.getElementsByTagName(tag).length; for (i = 0 ; i < tag_len ; i++){ document.getElementsByTagName(tag)[i].style.backgroundColor='rgb('+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+')'; } } // 数字随机切换函数 function changNumber(tag){ var tag_len = document.getElementsByTagName(tag).length; for (i = 0 ; i < tag_len ; i++){ document.getElementsByTagName(tag)[i].innerHTML=Math.floor(Math.random()*11);//←数字,所以不用"" } } $('document').ready(function () { // 鼠标点击按钮时同时出发数字变化和颜色变化 $('button').click(function () { changColor('a'); changNumber('a'); }); // 鼠标移动到图像上变形的同时附加阴影效果 $('a').mousemove(function () { $bgColor = $(this).css('backgroundColor');//把获取到的颜色值赋值给变量 $(this).css('box-shadow','0px 0px 20px '+$bgColor); $(this).css('border-radius','20px'); }); //鼠标移开图像恢复原样 $('a').mouseleave(function () { $bgColor = $(this).css('backgroundColor'); $(this).css('box-shadow','none'); $(this).css('border-radius','50px'); }); }); </script> </head> <body> <a href="">1</a> <a href="">2</a> <a href="">3</a> <a href="">4</a> <br> <button>变色按钮</button> </body> </html>