如何使用 jQuery append() 执行广泛的 DOM 插入(以正确的顺序)?

发布于 2024-10-04 08:44:00 字数 1566 浏览 0 评论 0原文

当我使用 jQuery append() 方法时,生成的 HTML 输出不符合我想要的顺序。

正如您在下面的测试用例中看到的那样,每当我打开 DIV 标记而不关闭它时,它都会关闭。

    标签也是如此 - 它也在我预期之前关闭 - 我希望它在第二个
  • 标签之后关闭嵌套在它下面。

按照编写顺序构建大型动态 HTML 块的最佳方法是什么?

 $('#a').remove(); // remove #a from the dom and re-insert it dynamically
 $("<div id='a'>").insertAfter('div#main');

 $('#a').append("  <ul>");
 $('#a').append("    <li>A</li>");
 $('#a').append("    <li>B</li>");
 $('#a').append("  </ul>"); 
 $('#a').append("  <div id='X1'>");
 $('#a').append("    <div id='b'>");
 $('#a').append("      <div id='b1'></div>");
 $('#a').append("      <div id='b2'></div>");
 $('#a').append("    </div>");
 $('#a').append("    <div id='c'>");
 $('#a').append("      <p id='c1'></p>");
 $('#a').append("    </div>");
 $('#a').append("  </div>");
 $('#a').append("  <div id='X2'>B</div>");
 $('#a').append("</div>");

 <div id="main>
  <div id="a>
   <ul></ul> // <ul> tag is closed!?
   <li>A</li>
   <li>B</li>
   <div id='X1'></div> // div is closed!?
   <div id='b'> // div is closed!?
   <div id='b1'></div>
   <div id='b2'></div>
   <div id='c'></div> // div is closed!?
   <p id='c1'></p>
   <div id='X2'>B</div>
  </div>
 </div>

When I use the jQuery append() method, the resulting HTML output is not in the order I intend.

As you see in the test case below, any time I open a DIV tag without closing it, it is closed nevertheless.

The same goes for the <ul> tag - which was also closed before I expected - I wanted it to be closed after the second <li> tag that is supposed to be nested below it.

What's the best way to build a large block of dynamic HTML in the order it is written?

 $('#a').remove(); // remove #a from the dom and re-insert it dynamically
 $("<div id='a'>").insertAfter('div#main');

 $('#a').append("  <ul>");
 $('#a').append("    <li>A</li>");
 $('#a').append("    <li>B</li>");
 $('#a').append("  </ul>"); 
 $('#a').append("  <div id='X1'>");
 $('#a').append("    <div id='b'>");
 $('#a').append("      <div id='b1'></div>");
 $('#a').append("      <div id='b2'></div>");
 $('#a').append("    </div>");
 $('#a').append("    <div id='c'>");
 $('#a').append("      <p id='c1'></p>");
 $('#a').append("    </div>");
 $('#a').append("  </div>");
 $('#a').append("  <div id='X2'>B</div>");
 $('#a').append("</div>");

 <div id="main>
  <div id="a>
   <ul></ul> // <ul> tag is closed!?
   <li>A</li>
   <li>B</li>
   <div id='X1'></div> // div is closed!?
   <div id='b'> // div is closed!?
   <div id='b1'></div>
   <div id='b2'></div>
   <div id='c'></div> // div is closed!?
   <p id='c1'></p>
   <div id='X2'>B</div>
  </div>
 </div>

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

九歌凝 2024-10-11 08:44:00

这里要记住的重要一点是 .append() 不会附加 HTML,它附加由您传入的 HTML 生成的 DOM 元素,因此例如

    将是

      ; 元素就像你看到的那样。

    要使其间隔与当前的间隔相似,您需要附加每一行或使用 \ ,如下所示:

    $("<div id='a'>\
        <ul> \
           <li>A</li> \
           <li>B</li> \
        </ul> \
        <div id='X1'> \
         <div id='b'> \
           <div id='b1'></div> \
           <div id='b2'></div> \
         </div> \
         <div id='c'> \
           <p id='c1'></p> \
         </div> \
        </div> \
       <div id='X2'>B</div> \
      </div>").insertAfter('div#main');
    

    您可以在这里测试一下

    The important thing to remember here is that .append() doesn't append HTML, it appends DOM elements generated by the HTML you pass in, so for example <ul> will be a <ul></ul> element like you're seeing.

    To have it spaced out similar to what you have currently, you either need to append each line or use \ like this:

    $("<div id='a'>\
        <ul> \
           <li>A</li> \
           <li>B</li> \
        </ul> \
        <div id='X1'> \
         <div id='b'> \
           <div id='b1'></div> \
           <div id='b2'></div> \
         </div> \
         <div id='c'> \
           <p id='c1'></p> \
         </div> \
        </div> \
       <div id='X2'>B</div> \
      </div>").insertAfter('div#main');
    

    You can test it out here.

    許願樹丅啲祈禱 2024-10-11 08:44:00

    使用简单的 StringBuffer 实现,如下所示。一旦 html 完全离线缓冲,立即将其写入 DOM:

    function StringBuffer() {
       this.buffer = [];
     }
    
     StringBuffer.prototype.append = function append(string) {
       this.buffer.push(string);
       return this;
     };
    
     StringBuffer.prototype.toString = function toString() {
       return this.buffer.join("");
     };
    
     var buf = new StringBuffer();
     buf.append('<ul>');
     buf.append('<li>A</li>');
     buf.append('</ul>');
     ...etc...
     $("#a").empty().html(buf.toString());
    

    并参见:http://developer.yahoo.com/performance/rules.html

    Use a simple StringBuffer implementation, such as the one below. Once the html has been fully buffered offline, write it to the DOM all at once:

    function StringBuffer() {
       this.buffer = [];
     }
    
     StringBuffer.prototype.append = function append(string) {
       this.buffer.push(string);
       return this;
     };
    
     StringBuffer.prototype.toString = function toString() {
       return this.buffer.join("");
     };
    
     var buf = new StringBuffer();
     buf.append('<ul>');
     buf.append('<li>A</li>');
     buf.append('</ul>');
     ...etc...
     $("#a").empty().html(buf.toString());
    

    And see: http://developer.yahoo.com/performance/rules.html

    绅士风度i 2024-10-11 08:44:00

    您需要首先构建完整的字符串,然后将其传递给附加:

     // this creates the div with id 'a', and inserts it into 
     // the DOM (assuming you have an existing div with id 'main')
     //
     $("<div id='a'></div>").insertAfter('div#main');
    
     // now you can build up additional content as a string...
     //
     var s = "  <ul>";
     s += "    <li>A</li>";
     s += "    <li>B</li>";
     s += "  </ul>"; 
     s += "  <div id='X1'>";
     s += "    <div id='b'>";
     s += "      <div id='b1'></div>";
     s += "      <div id='b2'></div>";
     s += "    </div>";
     s += "    <div id='c'>";
     s += "      <p id='c1'></p>";
     s += "    </div>";
     s += "  </div>";
     s += "  <div id='X2'>B</div>";
    
     // now, use the append() function to create DOM from the string,
     // and append it to the content of your div#a all at once.
     //
     $('#a').append(s);
    

    有更有效的方法来构建该字符串,但是......您明白了。

    You need to build the complete string first, then pass that to append:

     // this creates the div with id 'a', and inserts it into 
     // the DOM (assuming you have an existing div with id 'main')
     //
     $("<div id='a'></div>").insertAfter('div#main');
    
     // now you can build up additional content as a string...
     //
     var s = "  <ul>";
     s += "    <li>A</li>";
     s += "    <li>B</li>";
     s += "  </ul>"; 
     s += "  <div id='X1'>";
     s += "    <div id='b'>";
     s += "      <div id='b1'></div>";
     s += "      <div id='b2'></div>";
     s += "    </div>";
     s += "    <div id='c'>";
     s += "      <p id='c1'></p>";
     s += "    </div>";
     s += "  </div>";
     s += "  <div id='X2'>B</div>";
    
     // now, use the append() function to create DOM from the string,
     // and append it to the content of your div#a all at once.
     //
     $('#a').append(s);
    

    there are more efficient ways to build that string, but... you get the idea.

    烟花易冷人易散 2024-10-11 08:44:00

    jQuery 附加设计用于处理完整的 HTML 片段。只需将您的代码修改为所有append()一次,然后将所有代码连接在一起传递给它。如果不这样做,append() 将自动关闭所有打开的标签。

    jQuery append is designed to work with a complete snippet of HTML. Modify your code to all append() only once and pass it all your code concatenated together. If you do not, append() will close any open tags automatically.

    温柔戏命师 2024-10-11 08:44:00
    <script>
    $('#a').html(your_html);
    </script>
    
    <script>
    $('#a').html(your_html);
    </script>
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文