摘要:<!DOCTYPE html><html><head><meta charset="utf-8" /><title>Page Title</title><script src="https://code.jquery.com/jquery-3.1.1.min.js"><
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Page Title</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<style>
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>
<body>
<a href=""></a>
<a href=""></a>
<a href=""></a>
<a href=""></a>
</body>
</html>
<script>
//改變標簽背景色的函數(shù)
function changecolor(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)+')';
}
}
$(function(){
changecolor('a');
$('a').mouseover(function(){
$bg = $(this).css('backgroundColor');
$(this).css('box-shadow','0px 0px 50px '+$bg);
$(this).css('border-radius','20px');
})
$('a').mouseleave(function(){
$(this).css('box-shadow','none');
$(this).css('border-radius','50px');
})
})
</script>
//Math.random獲取的是一個0到1的隨機值
批改老師:天蓬老師批改時間:2019-01-24 17:26:02
老師總結(jié):Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)
Math是js內(nèi)置的重要對象之一, 可以實現(xiàn)很多事情,特別是一些動畫效果,必須用到,多多練習