是否可以在JavaScript中使用Document Fragment在JavaScript中创建嵌套元素?

发布于 2025-01-23 10:59:39 字数 710 浏览 3 评论 0原文

我想使用DocumentFragment创建一个可重复使用的HTML块。这个想法是为现有数组中的每个对象创建并填充类似于下面的HTML代码的块。我不担心填充内部HTML,现在我只想建立一个嵌套的层次结构。

<div class="container">
    <div class="titleABC">
        <h2 class="abc"></h2>
        <h2 class="efg"></h2>
        <h2 class="xyz"></h2>
        ...
        ...
        ...
        ...
    </div>
</div>

我已经将我正在处理的代码添加到 js fiddle

在文档片段中,我无法获得多个元素,并且想知道这是否可能。或者,如果有一种更好的方法来实现这一目标,则将朝着正确的方向发展。

最终,我希望能够添加像... 我的JS小提琴中的第16、17、18、19行,并在将其添加到数组中就具有一个代码块。

预先感谢您浪费时间来帮助我解决我的胡说八道。

I would like to create a reuseable html block using documentFragment. The idea is to create and fill a block of html code similar to below, for every object in an existing array. I'm not worried about filling the inner html yet, for now I just want to build a nested hierarchy.

<div class="container">
    <div class="titleABC">
        <h2 class="abc"></h2>
        <h2 class="efg"></h2>
        <h2 class="xyz"></h2>
        ...
        ...
        ...
        ...
    </div>
</div>

I've add the code I was working on to a js Fiddle.

I could not get more than one element to work in the document Fragment and was wondering if this is possible. Or if there is a better way to accomplish this, a steer in the right direction would be appreciated.

Ultimately I want to be able to add code like...
line 16, 17, 18, 19 in my js fiddle, and have a code block generated simply from having added it to the array.

Thank you in advance for time you waste helping me with my nonsense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文