js点击获取随机颜色值

Original 2019-02-13 22:17:57 266
abstract:<!DOCTYPE html><html><head> <title>点击获取随机颜色</title> <script src="http://code.jquery.com/jquery-3.1.1.min.js"></script> <style type="text/css

<!DOCTYPE html>

<html>

<head>

<title>点击获取随机颜色</title>

<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>

<style type="text/css">

*{margin: 0;padding: 0;}

a{

float: left;

display: block;

width: 150px;

height: 80px;

line-height: 80px;

text-align: center;

text-decoration: none;

border-radius: 10px;

color: #fff;

margin: 50px;

}

button{float: left;margin-top: 180px;margin-left: -122px;}

</style>

</head>

<body>

<a href="javascript:;" onclick="randBg(this)">0</a>

<a href="javascript:;" onclick="randBg(this)">0</a>

<a href="javascript:;" onclick="randBg(this)">0</a>

<a href="javascript:;" onclick="randBg(this)">0</a>

</body>

</html>

<script type="text/javascript">

// 每次刷新浏览器随机给小球添加颜色

function setBg(tag){

var len = document.getElementsByTagName(tag).length;

console.log(len);

for(var i=0; i<len; i++){

document.getElementsByTagName(tag)[i].style.backgroundColor = 'rgb('+Math.floor(Math.random()*255)+','+Math.floor(Math.random()*255)+','+Math.floor(Math.random()*255)+')';

}

}


// 点击按钮改变小球的颜色

function randBg(obj){

obj.style.backgroundColor = 'rgb('+Math.floor(Math.random()*255)+','+Math.floor(Math.random()*255)+','+Math.floor(Math.random()*255)+')';

var bg = obj.style.backgroundColor;

console.log(bg);

obj.innerHTML = bg;

}


$(document).ready(function(){

setBg('a');


$('a').mouseover(function(){

$bg = $(this).css('backgroundColor');

$(this).css('boxShadow','0px 0px 20px '+$bg);

});


$('a').mouseleave(function(){

$(this).css('boxShadow','none');

$(this).css('borderRadius','10px');

});

});

</script>

QQ图片20190213221616.png

Correcting teacher:灭绝师太Correction time:2019-02-14 09:13:59
Teacher's summary:完成的不错,代码还可以简化哦,继续加油!

Release Notes

Popular Entries