<i id='ZAw8K'><tr id='ZAw8K'><dt id='ZAw8K'><q id='ZAw8K'><span id='ZAw8K'><b id='ZAw8K'><form id='ZAw8K'><ins id='ZAw8K'></ins><ul id='ZAw8K'></ul><sub id='ZAw8K'></sub></form><legend id='ZAw8K'></legend><bdo id='ZAw8K'><pre id='ZAw8K'><center id='ZAw8K'></center></pre></bdo></b><th id='ZAw8K'></th></span></q></dt></tr></i><div id='ZAw8K'><tfoot id='ZAw8K'></tfoot><dl id='ZAw8K'><fieldset id='ZAw8K'></fieldset></dl></div>
    <legend id='ZAw8K'><style id='ZAw8K'><dir id='ZAw8K'><q id='ZAw8K'></q></dir></style></legend>
    • <bdo id='ZAw8K'></bdo><ul id='ZAw8K'></ul>
    <tfoot id='ZAw8K'></tfoot>

    <small id='ZAw8K'></small><noframes id='ZAw8K'>

      如何打造“大"JavaScript(JQuery)中的div结构?

      时间:2023-10-20

      <small id='hgKp5'></small><noframes id='hgKp5'>

      <legend id='hgKp5'><style id='hgKp5'><dir id='hgKp5'><q id='hgKp5'></q></dir></style></legend>

          <tbody id='hgKp5'></tbody>
          • <bdo id='hgKp5'></bdo><ul id='hgKp5'></ul>
          • <i id='hgKp5'><tr id='hgKp5'><dt id='hgKp5'><q id='hgKp5'><span id='hgKp5'><b id='hgKp5'><form id='hgKp5'><ins id='hgKp5'></ins><ul id='hgKp5'></ul><sub id='hgKp5'></sub></form><legend id='hgKp5'></legend><bdo id='hgKp5'><pre id='hgKp5'><center id='hgKp5'></center></pre></bdo></b><th id='hgKp5'></th></span></q></dt></tr></i><div id='hgKp5'><tfoot id='hgKp5'></tfoot><dl id='hgKp5'><fieldset id='hgKp5'></fieldset></dl></div>

              1. <tfoot id='hgKp5'></tfoot>

                本文介绍了如何打造“大"JavaScript(JQuery)中的div结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我需要制作大的 DIV 结构并将它们附加到包装器中.但到目前为止,我所看到的总是将 一个 DIV 元素附加到另一个元素中.

                I need to make big DIV structures and append them to a wrapper. But what I've seen so far was always about appending one DIV element into another one.

                有一个空的 DIV-Wrapper,用大"DIV-Elements 填充

                There is an empty DIV-Wrapper, to be filled with "big" DIV-Elements

                <div class="PostWrapper">
                    // Content should be added to here
                </div>
                

                我想附加到 PostWrapper 的 DIV 元素如下所示:

                The DIV-Elements which I want to append to the PostWrapper look like this:

                <div class="Post">
                    <div class="PostHead">
                         <span class="profilePicture">
                             <img src="../Profiles/Tom.jpg" />
                         </span>
                         <span class="userName">Tom</span>
                         <span class="postTime">10 minutes ago</span>
                    </div>
                    <div class="PostBody">
                        <p>Hey Tom, great work at the presentation last week. Well done!</p>
                    </div>
                </div>
                

                所以这是一个 DIV 结构的示例,我想构建并存储在一个 javascript 变量中并附加到我的包装器中,例如:

                So this is an example of a DIV-Structure which I'd like to build and store in a javascript Variable and append to my wrapper like:

                var postElement = $("div", {class: "Post"}).append("div", {class: "PostHead"}).append("div", {class: "PostBody"})......
                

                这不会像预期的那样工作.但是我想不出一个简单的方法来避免过于复杂的代码.

                This wont work like expected ofc. But I can't think of a simple way of doing this without having overly complexe code.

                这怎么可能?

                推荐答案

                对于任何复杂的东西,使用 HTML 模板.这可以像一个带有 id 的虚拟脚本块一样简单,它使用未知类型,因此浏览器会忽略它(我使用文本/模板).然后,您只需使用元素的内部 html() 来创建新结构.更容易阅读/维护,更不容易出错

                For anything complex, use a HTML template. That can be as simple as a dummy script block, with an id, that uses an unknown type so the browser ignore it (I use text/template). You then just use the inner html() of the element to create the new structure. Much easier to read/maintain and less error prone

                示例 html:

                <script id="mytemplate" type="text/template">
                    <div class="Post">
                        <div class="PostHead">
                             <span class="profilePicture">
                                 <img src="../Profiles/Tom.jpg" />
                             </span>
                             <span class="userName">Tom</span>
                             <span class="postTime">10 minutes ago</span>
                        </div>
                        <div class="PostBody">
                            <p>Hey Tom, great work at the presentation last week. Well done!</p>
                        </div>
                    </div>
                <script>
                

                然后创建一个:

                $('.PostWrapper').append($('#mytemplate').html());
                

                注意:如果他们在任何地方需要动态值,请在模板中使用文本替换标记并替换字符串.

                例如

                <script id="mytemplate" type="text/template">
                    <div class="Post">
                        <div class="PostHead">
                             <span class="profilePicture">
                                 <img src="{image}" />
                             </span>
                             <span class="userName">{name}</span>
                             <span class="postTime">{posted}</span>
                        </div>
                        <div class="PostBody">
                            <p>{notes}</p>
                        </div>
                    </div>
                <script>
                

                或者在模板添加到 DOM 后简单地添加这些细节.

                Or simply add those details once the template has been added to the DOM.

                这篇关于如何打造“大"JavaScript(JQuery)中的div结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何删除鼠标离开的下一个附加? 下一篇:是否可以在不修改原始代码的情况下向现有的 javascript 函数添加一些代码?

                相关文章

              2. <i id='tG8YA'><tr id='tG8YA'><dt id='tG8YA'><q id='tG8YA'><span id='tG8YA'><b id='tG8YA'><form id='tG8YA'><ins id='tG8YA'></ins><ul id='tG8YA'></ul><sub id='tG8YA'></sub></form><legend id='tG8YA'></legend><bdo id='tG8YA'><pre id='tG8YA'><center id='tG8YA'></center></pre></bdo></b><th id='tG8YA'></th></span></q></dt></tr></i><div id='tG8YA'><tfoot id='tG8YA'></tfoot><dl id='tG8YA'><fieldset id='tG8YA'></fieldset></dl></div>

              3. <tfoot id='tG8YA'></tfoot>

                  <small id='tG8YA'></small><noframes id='tG8YA'>

                1. <legend id='tG8YA'><style id='tG8YA'><dir id='tG8YA'><q id='tG8YA'></q></dir></style></legend>
                    <bdo id='tG8YA'></bdo><ul id='tG8YA'></ul>