且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JavaScript:我应该如何生成大量HTML?

更新时间:2023-11-10 10:59:04

将代码段创建为模板,并将其放入不可见的<div>:

Create snippets as templates, put them into an invisible <div>:

<div style="display: none">
   <div id="template1">
      <h2 class="header_identifyingClass">Hello from template</h2>
   </div>
   <div id="template2">
      <span class="content">Blah blah</span>
   </div>
</div>

然后找到它,

document.getElementById("template1"); 

填写其内部值,例如通过XPath或jQuery查找内部元素并填充它们,例如使用element.innerHTML = "Hello from new value",并将其移动或复制到DOM的可见部分.

fill it's internal values, e.g. find inside elements by XPath or jQuery and fill them e.g. using element.innerHTML = "Hello from new value", and move or copy it to the visible part of DOM.

创建多个模板并多次复制以生成许多模板. 不要忘记更改副本的ID以保持其正常工作.

Create multiple templates and copy it multiple times to generate many. Don't forget to change the ID for copies to keep it working.

PS:我想我在

PS: I think I used this approach in the code of JUnitDiff project. But it's buried in XSLT which serves another purpose.