我有以下代碼:正確的方式來添加HTML將jQuery/JavaScript的
<section class="section1">
//Section one content
</section>
<section class="section2">
<h4>Some Title</h4>
<input type="hidden" class="keys" value="1"/>
<div id="block-close-1"></div>
<div class="block" id="block-1">
//Block content
</div>
</section>
<section class="section3">
//Section3 content
</section>
我想要做的是採取一些html和「塊1」
的HTML我後插入要添加看起來像這樣:
<input type="hidden" class="keys" value="2"/>
<div id="block-close-2"></div>
<div class="block" id="block-2">
//Block content
</div>
我已經試過這樣:
var html = '//code as above';
$("html").insertAfter("#block-1");
我得到的錯誤是這樣的:
Uncaught HierarchyRequestError: Failed to execute 'insertBefore' on 'Node': The new child element contains the parent.
我也試過這樣: 的document.getElementById( '#塊4')的appendChild(HTML);
錯誤:
Uncaught TypeError: Cannot read property 'appendChild' of null
什麼是新的HTML添加到現有的HTML的正確方法?
在你的情況下'html'是一個變量,所以你必須使用'$(html).insertAfter(「#block-1」);'(不含引號(如@Elentriel回答)這個'$(「