Add div above another div
<div>
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Thr开发者_运维知识库ee</div>
<div class="four">Four</div>
<div class="five">Five</div>
</div>
I need to add a div between div Three and Four, but I can't use any sort of targeting on the parent div (only the divs inside it).
jQuery('.four').parent().prepend('<div class="addme">Add Me!</div>');
This as you probably know adds it to the top, above div One. Without the ".parent()" it adds the div inside of div Four, before the content. Same difference for ".append()".
Anyone got a solution?
You can use .before()
or .after()
like this:
jQuery('.four').before('<div class="addme">Add Me!</div>');
//or...
jQuery('.three').after('<div class="addme">Add Me!</div>');
$('.three').after('<div class="addme">Add Me!</div>');
精彩评论