JavaScript增加一个随机颜色的div,并在一定时间后div自动消失
小女新手学习中,这里将会整理和记录小女的学习笔记,如有错误的地方希望各位前辈多多指教,小女在此不甚感激,一定虚心受教。
我的邮箱:lf_radish@163.com
1 function Panel(_width, _height) { 2 this.width = _width; 3 this.height = _height; 4 5 this.randomColor = function () { 6 var r = parseInt(Math.random() * 255).toString(16);//转换为16进制的数 7 var g = parseInt(Math.random() * 255).toString(16); 8 var b = parseInt(Math.random() * 255).toString(16); 9 return "#" + r + g + b; 10 } 11 this.create = function () { 12 this.div = document.createElement("div"); 13 this.div.style.width = this.width + "px"; 14 this.div.style.height = this.height + "px"; 15 this.div.style.backgroundColor = this.randomColor(); 16 document.body.appendChild(this.div); 17 var temp = this; 18 setTimeout(function () {//3秒后这个div消失 19 document.body.removeChild(temp.div) 20 }, 3000); 21 } 22 } 23 24 25 26 <div id="demo"> 27 <input id="Button1" type="button" value="button" onclick="javascript:new Panel(200,100).create();" /> 28 </div>