简体   繁体   English

是否可以将带有子元素的 html 元素保存为 JS 变量,您可以动态地将 append 保存到 DOM 中?

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

相关问题 如何将 append 多个 img-child 元素添加到一个带有 JS 的 DOM 元素中? - how to append multiple img-child elements to a DOM element with JS? 将属性保存到变量并将 append 保存到 DOM 元素 - Save property to variable and append it to DOM element 您如何检测html元素是否可以附加子节点? - How do you detect if an html element can append child nodes? 为什么 append 不可能在两个 dom 元素中使用同一个子元素? - Why isn't possible to append a same child in two dom element? 如何在元素P中获取文本,用javascript DOM排除它们可能的子元素A? - How can I get the text inside an element P excluding their possible child elements A with javascript DOM? 是否可以使用Javascript中的DOM将数据附加到html元素中? - Is it possible to append data into a html element using DOM in Javascript? 我可以使用innerHTML将所有可能的HTML元素附加到哪个HTML元素? - To what HTML element can I append all possible HTML elements using innerHTML? 哪种渲染图像操作更快:HTML5 canvas元素,或使用JS / jQuery处理DOM元素的CSS? - Which is faster for rendering image manipulations: HTML5 canvas element, or manipulating DOM elements' CSS with JS/jQuery? 无法将child附加到我的数组中的每个DOM元素? - Can't append child to every DOM element in my array? 使用不带子元素的javascript将HTML元素DOM作为字符串获取 - Getting a HTML element DOM as string using javascript without child elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM