![](/img/trans.png)
[英]how to append multiple img-child elements to a DOM element with JS?
[英]Is it possible to save an html element with child elements as JS variable which you can dynamically append to the DOM?
我有这个表单片段
<div id="form-container" class="row">
<select style="width:250px;" name="drug_pack_size" id="drug_pack_size" class="form-control">
<option value="">Selling Unit</option>
<option value="Ampoule">Ampoule</option>
</select>
<span class="d-flex">
<p class="mx-3">contains</p>
<input type="text" class="form-control">
</span>
<span class="d-flex">
<p class="mx-3">for</p>
<input type="text" class="form-control">
<p class="mx-3">GHC</p>
</span>
</div>
有没有可能做这样的事情
const form = document.querySelector('#form-element').content
这样您就可以使用 JS 代码反复动态地 append 它,而无需从 JS 后端编写所有内容
对的,这是可能的。 请试试
var x = document.getElementById("form-container").outerHTML;
window.alert(x);
在此捕获之后,如果您想重复添加相同的父元素,则包含变量以增加父元素和子元素的id
和/或name
以防止重复。 例如, id="form-container1"
、 id="form-container2"
、 id="form-container3"
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.