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

时间:2010-11-27 23:12:42

标签: jquery html append dom-manipulation

当我使用jQuery append()方法时,生成的HTML输出不符合我的意图。

正如您在下面的测试用例中看到的那样,只要我打开DIV标记而不关闭它,它就会被关闭。

同样适用于<ul>标记 - 它在我预期之前也已关闭 - 我希望它在第二个<li>标记之后关闭,该标记应该嵌套在它下面。

按照编写顺序构建大块动态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>

5 个答案:

答案 0 :(得分:3)

要记住的重要一点是,.append()不附加HTML,它附加了您传入的HTML生成的 DOM元素,因此例如<ul>将像你看到的一样<ul></ul>元素。

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

$("<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

答案 1 :(得分:3)

使用简单的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

答案 2 :(得分:1)

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

 // 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);

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

答案 3 :(得分:0)

jQuery append旨在使用完整的HTML片段。将您的代码修改为all append()仅一次,并将所有代码连接在一起传递给它。如果不这样做,append()将自动关闭所有打开的标签。

答案 4 :(得分:0)

<script>
$('#a').html(your_html);
</script>