让JavaScript客户端div在父div容器内生成

时间:2014-04-23 12:55:32

标签: javascript html dom

所以我编写了一些JS代码,每隔几毫秒创建一个新的div并将它们堆叠起来。但我希望这个脚本发生在DIV的空间内。到目前为止,脚本在任何div容器之外运行。 是document.getElementById(“parent-div”)。innerHTML = scriptFunction();正确的解决方案?

<script>
//where to begin. 
//wrap code in functions and load them up via an DOM event handler. 
//aim is too eliminate the function when the array collects 30 elements/data types. 


var collect = []; //this array has global scope. because it's..
//been declared using var outside of a function. 

window.onload = function(){

var holder = function(){


var invent = function(){

//var create = document.createElement("div").setAttribute("id","posts");

var d = document.createElement("div"); 
  d.style.width="350px"; 
  d.style.height="100px"; 
  d.style.border="1px solid #333"; 
  d.style.cssFloat="left"; 
var random = Math.random();    

   i = 1; 
collect.push(i); 
var content = ["Bogus",
"<p>lala haha</p>", "<p>sooooooooooooooooooooooooooooooo</p>", "<P> foxy lady sinks into the bed</P>" ]; 
  d.innerHTML = collect.length + content[collect.length]; 
document.body.appendChild(d); //appendChild?


if(collect.length == 5){

alert("ahhhh 30"); //could trigger a new function. 

}

}; //end of invent. 

setInterval(invent,200); 




}; //end of holder function. 


//ok now i want this script to happen inside the "roll-out" div so i've tried this:
document.getElementById("roll-out").innerHTML=holder(); 

};
</script>

1 个答案:

答案 0 :(得分:0)

尝试更改

document.body.appendChild(d); //appendChild?

类似

document.getElementById("parent-div").appendChild(d);