2013-08-19 159 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

相关问题