<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> #circle{position:relative;width: 600px; height: 600px;background: #ff0000;border-radius: 50%;margin:0 auto;} .c{position: absolute; width: 50%; height: 50%;background: #00ff00;border-radius: 50%;} </style> </head> <body> <div id="circle"> <!--<div></div>--> </div> <script> var circleObj=document.getElementById("circle"); var cObj=circleObj.getElementsByTagName("div"); circleObj.onmouseenter=function(){ fenLie(this) }; function fenLie(obj){ var frag=document.createDocumentFragment();//建立碎片至關於一個空容器,將div打包入這個frag for(var i=0;i<4;i++){ var div=document.createElement("div"); if(i==0){ div.style.left="0px"; div.style.top="0px"; } else if(i==1){ div.style.right="0px"; div.style.top="0px"; } else if(i==2){ div.style.left="0px"; div.style.bottom="0px"; } else if(i==3){ div.style.right="0px"; div.style.bottom="0px"; }//使用定位,兼容IE7 div.className="c"; div.style.background=color(); div.onmouseenter=function(){ fenLie(this);} frag.appendChild(div);//在最後插入div } obj.appendChild(frag); obj.style.background="none"; obj.onmouseenter=null; }//解綁 function color(){ var arr=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];//顏色是十六進制 var str="#"; for(var i=0;i<6;i++){ var r=Math.floor(Math.random()*16);//產生0-15之間的隨機數; str+=arr[r];//隨機選擇在數組的第幾位,而且拼接起來 } return str; } // } </script> </body> </html>