jquery
2012-04-10 162 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>'); 
相關問題