<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title> JS learning dynamic Create HTML elements </title> <style><!--body{margin:0;padding:0;} . Sky{background: #000; width:1000;height:500px; position:relative;} #s {background: #FFF; width:1px; height:1px; overflow:hidden; position:absolute;} --></style><style bogus= "1" >body{margin:0;padding:0;} . Sky{background: #000; width:1000;height:500px; position:relative;} #s {background: #FFF; width:1px; height:1px; overflow:hidden; position:absolute;} </style> </pead> <body> <div class= "Sky" id= "Sky" ></div> <button id= "Creatstar" Oncl Ick= "Creatstars ()" > Nothing stars play </button> <script type= "text/javascript" ><!--function $ (ID) {return document.getElementById (ID); function Creatstars () {var star = document.createelement ("div"); Star.id = "S"; var posx = Math.floor (Math.random (star.style.top) *400+100); var posy = Math.floor (Math.random (star.style.left) *1000+1); $ ("Sky"). AppendChild (Star); Star.style.top = posx + "px"; Star.style.left = posy + "px"; }//--></script> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]