开发者

Add div above another div

开发者 https://www.devze.com 2023-01-19 19:16 出处:网络
<div> <div class=\"one\">One</div> <div class=\"two\">Two</div> <div class=\"three\">Thr开发者_运维知识库ee</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>');
0

精彩评论

暂无评论...
验证码 换一张
取 消