简体   繁体   中英

insert new DOM between elements

I have the following part of DOM:

<div id="content">
  <div id="div-1"></div>
  <!-- here new DOM -->
  <div id="div-2"></div>
</div>

I need to insert new part of DOM between "div-1" and "div-2".

How I can do it with jQuery?

use .after() on div-1

$('#div-1').after('<div id="new">new div</div>');

or use .before() on div-2

$('#div-2').before('<div id="new">new div</div>');
$('#div-1').after('<div>new content</div>')'

要么

$('<div>new content</div>').insertAfter($('#div-1'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM