簡體   English   中英

使JavaScript客戶端div在父div容器內生成

[英]getting JavaScript client side divs to spawn inside of a parent div container

所以我寫了一些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>

嘗試改變

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM