[英]Is it possible to save an html element with child elements as JS variable which you can dynamically append to the DOM?
I have this form snippet我有这个表单片段
<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>
Is it possible to do something like this有没有可能做这样的事情
const form = document.querySelector('#form-element').content
Such that you can append it repeatedly and dynamically with JS code, without writing everything from the JS Backend这样您就可以使用 JS 代码反复动态地 append 它,而无需从 JS 后端编写所有内容
Yes, it is possible.对的,这是可能的。 Please try
请试试
var x = document.getElementById("form-container").outerHTML;
window.alert(x);
After this capture, if you want to repeatedly add same parent Element, then include variable(s) to increment the id
and/or name
of the parent and child elements to prevent duplication.在此捕获之后,如果您想重复添加相同的父元素,则包含变量以增加父元素和子元素的
id
和/或name
以防止重复。 For example, id="form-container1"
, id="form-container2"
, id="form-container3"
, and so on.例如,
id="form-container1"
、 id="form-container2"
、 id="form-container3"
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.