![](/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.