简体   繁体   English

javascript添加子元素和父元素

[英]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.

相关问题 在一组匹配元素上将父元素添加到父级,并使用Parent Div href - Javascript - Add Child Element to Parent on a Set of Matched Elements, and use Parent Div href - Javascript 使用javascript从子元素创建父对象 - Create a parent from child elements using javascript Javascript - 从数组中删除子元素或父元素 - Javascript - remove child or parent elements from an array 如果任何子元素具有某个类,则将类添加到父div,否则将其删除(jQuery或Javascript) - Add class to parent div if any of the child elements has a certain class, remove if not (jQuery or Javascript) 将父 ID 添加到 Javascript 中的每个嵌套子级 - Add Parent Id toEvery Nested Child in Javascript 如何在javascript中将子数组添加到父数组? - How to add a child array to a parent array in javascript? 如何在javascript html中为父/子添加节点 - How to add nodes for parent/child in javascript html 使用JavaScript和/或TinySort按子元素的条件对父元素进行排序 - Sort Parent Elements by criteria of Child Elements with JavaScript and / or TinySort jQuery / JavaScript-计算多个父元素中的子元素 - jQuery/JavaScript - Counting child elements in multiple parent elements 将相同的子元素添加到jQuery中的两个不同的父元素是否有效? - Is it valid to add the same element as a child to two different parent elements in jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM