<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>7-76 课堂演示</title>
<link rel="stylesheet" type="text/css" href="style.css">
<style type="text/css">
div{
margin: 15px auto;
width: 200px;
height: 200px;
padding: 15px;
border-radius: 200px;
position: absolute;
}
</style>
</head>
<body>
<!-- <div id="div1"></div> -->
<script>
// var div1=document.getElementById('div1')
// function moveBall(e){
// var sy=document.documentElement.scrollTop||document.body.scrollTop
// div1.style.left=e.clientX+'px';
// div1.style.top=e.clientY+sy+'px';
// }
// document.onmousemove=moveBall
function myFun1(e){
var num=30;
var divs=document.getElementsByTagName('div')
var body=document.getElementsByTagName('body')[0]
for(var i=0;i<num;i++){
var rad=Math.floor(Math.random()*10)+'px'
divs[i]=document.createElement('div')
divs[i].style.background='rgb('+4*i+','+2*i+','+i+')'
divs[i].style.width=divs[i].style.height=rad
divs[i].style.position='absolute'
divs[0].style.left=e.clientX+'px';
divs[0].style.top=e.clientY+'px';
divs[i].style.left=e.clientX+Math.floor(Math.random()*10*i)+'px';
divs[i].style.top=e.clientY+Math.floor(Math.random()*10*i)+'px';
body.appendChild(divs[i])
}
}
document.onmousemove=myFun1
//document.onmousedown=myFun1
</script>
</body>
</html>
上面代码中的divs[i].style.background='rgb('+4*i+','+2*i+','+i+')'这一行出xian
报错
Uncaught TypeError: Cannot read property 'style' of undefined
at HTMLDocument.myFun1
怎么解决
anet
QQ_随意