Javascript:DIV AppendChild

谁说我不能喝 提交于 2019-12-19 02:28:48

问题


In the below code "objTo" is a div to which i need to insert multiple number of div. when i use the code for the first time its working.but on the next time its overwriting the existing code.

     <script>

var divtest= document.createElement("div");        
divtest.innerHTML = "<div>new div</div>"         
objTo.appendChild(divtest)
    </script>

Where am i going wrong?


回答1:


I have made a very simple working version for you :

http://jsfiddle.net/hQKy9/

Multiple clicks works the whole time :

Script

function addDiv() {
    var objTo = document.getElementById('container');
    var divtest = document.createElement("div");
    divtest.innerHTML = "new div";
    objTo.appendChild(divtest);
}

Html

<div id="container"></div>

<input type="button" onclick="addDiv();" value="Click here to add div"/>


来源:https://stackoverflow.com/questions/10328210/javascriptdiv-appendchild

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!