[英]javascript add child and parent elements
How to create dynamic image drawing, my javascript foreach element commands: 如何创建动态图像绘图,我的javascript foreach元素命令:
temp = document.getElementById(docElement);
temp.appendChild(document.createElement('div'));
temp.lastElementChild.innerText = 'text';
temp.appendChild(document.createElement('img'));
docElement is simple another div container it draws: docElement很简单,它绘制了另一个div容器:
<div id="docElement">
<div>text</div>
<img></img>
<div>text</div>
<img></img>
<div>text</div>
<img></img>
...
</div>
I wanted: 我想了:
<div id="docElement">
<div>text<img></img></div>
<div>text<img></img></div>
<div>text<img></img></div>
<div>text<img></img></div>
...
</div>
You are getting in out of the inside div coz you are appending the image to main div docElement
. 您正在将div图像附加到main div docElement
之外,进入内部div docElement
。 Simply store them in different variable and play with them whatever way you want like this 只需将它们存储在不同的变量中,然后按自己喜欢的方式与它们一起玩
temp = document.getElementById("docElement"); innerdiv = document.createElement('div'); temp.appendChild(innerdiv); innerdiv.innerText = 'text'; innerdiv.appendChild(document.createElement('img'));
<div id="docElement"> </div>
temp = document.getElementById(docElement);
var div = document.createElement('div');
div.innerText = 'text';
div.appendChild(document.createElement('img'));
temp.appendChild(div);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.