鼠标移过随机变换颜色
阿涛2019-06-10 16:33:26159 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript小案例</title>
<style type="text/css">
div{width: 100px;height: 100px;background-color: #ccc;margin-top: 10px;border-radius: 50px;display: inline-block;}
</style>
</head>
<body>
<div onmouseover="myOnclick(this)";></div>
<div onmouseover="myOnclick(this)";></div>
<div onmouseover="myOnclick(this)";></div>
<div onmouseover="myOnclick(this)";></div>
<div onmouseover="myOnclick(this)";></div>
<div onmouseover="myOnclick(this)";></div>
<div onmouseover="myOnclick(this)";></div>
<div onmouseover="myOnclick(this)";></div>
<div onmouseover="myOnclick(this)";></div>
<div onmouseover="myOnclick(this)";></div>
<div onmouseover="myOnclick(this)";></div>
<script type="text/javascript">
function myOnclick(x) {
var clr = getRandomColor( )
x.style.backgroundColor="#"+clr;
};
function getRandomColor( ) {
var rand = Math.floor(Math.random( ) * 0xFFFFFF).toString(16);
if(rand.length == 6){
return rand;
}else{
return getRandomColor();
}
};
</script>
</body>
</html>