有没有办法让这个更简单?在另一个孩子DIV之前在父DIV中创建一个DIV
$('#content').append('<div id="bar"></div>');
$('#bar').insertBefore('#footer');
$('#foo').appendTo('#bar');
这就是最终产品的样子,但我认为有更清晰的方法可以做到这一点。
<div id="content">
<div id="bar">
<div id="foo"></div>
</div>
<div id="footer"></div>
</div>
......好吧,我把它归结为两行。
$('<div id="bar"></div>').insertBefore('#footer');
$('#foo').appendTo('#bar');
有真不是一个理由追加到#内容,因为#footer的将永远存在。
我不认为['insertBefore()'](http://api.jquery.com/insertBefore/)做你认为*的事情;在你的使用中,它会在$('#footer')'元素前面插入'$('#bar')'作为兄弟。而你的完成代码表明'#bar'应该是'#footer'的* parent *。 – 2012-03-09 22:53:11
谢谢,刚刚发现。让我更新这个例子。好的更新。我错误地复制了这个例子。 – benblustey 2012-03-09 22:55:13