2013-08-19 114 views
0

我使用jQuery .html()但可以在不刪除其他HTML元素的情況下添加元素嗎?在不刪除現有內容的情況下添加元素

我的代碼看起來如此:

<div id="contentboxes"> 
    <div class="con1">content 1</div> 
    <div class="con1">content 2</div> 
</div> 

我曾用jQuery試過這樣:

$('#contentboxes').html('<div class="con3">Content 3</div>'); 

但這個命令刪除我的其他2盒,是它可以在不去除其它框中添加?

+2

追加(),前置(),的insertBefore(),insertAfter(),()之前,之後().....的[jquery的插入新元素插入表 – adeneo

+0

可能重複細胞沒有擦除其他元素](http://stackoverflow.com/questions/17536561/jquery-insert-new-element-into-table-cell-without-erasing-other-elements) –

+0

我建議總是看看文檔第一:http://api.jquery.com/category/manipulation/。 –

回答

5

使用.append()而不是.html()

+1

甚至[prepend()](http://api.jquery.com/prepend/) – Alvaro

+1

另外.prepend(),.after()和.before()可以是有用的 – cernunnos

+0

.prepend()是那個我需要,謝謝:) – user2496077

相關問題