在最后一个div jquery之前在另一个div中附加一个div

时间:2012-04-10 11:08:58

标签: jquery

假设我有像

这样的div
<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之前被推入主div。请帮我概念。

感谢

3 个答案:

答案 0 :(得分: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>");

答案 1 :(得分:1)

这样:

$('#main #child').before('<p>Something</p>');

答案 2 :(得分: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>");