jquery
2012-04-10 270 views 1 likes 
1

假设我有div的像附加在另一个DIV一个DIV最后一个div jQuery的前

<div id='main'> 
    <div id='child'>my static content</div> 
</div> 

$('#main').append("<div class='mycontent'>I'm new box by append</div>"); 
or 
$('#main').appendTo("<div class='mycontent'>I'm new box by append</div>"); 

我知道我可以使用的append()和appendTo方法插入到任何主要股利。但我需要知道,我如何插入内容到主div的子div之前。内容将在子div之前被推入主div。请帮助我的概念。

感谢

回答

2

http://api.jquery.com/before/

,如果你的最后一个子DIV之前希望:

$("#main > div:last-child").before("<div class='mycontent'>I'm new box by append</div>"); 

,或者如果您有一个特定的DIV之前希望:

$("#child").before("<div class='mycontent'>I'm new box by append</div>"); 
0

试试这个:

$('#child').before("<div class='mycontent'>I'm new box by append</div>"); 

也可能将是有益的:

$('#child').after("<div class='mycontent'>I'm new box by append</div>"); 
+1

错误的,因为这会前主,而不是最后一个div之前withing主要 – Gavriel 2012-04-10 11:12:51

+0

添加兴田新的div是的,谢谢修复 – Vytautas 2012-04-10 11:14:01

1

这样:

$('#main #child').before('<p>Something</p>'); 
相关问题