简体   繁体   中英

how to insert DOM element just after the div clicked

I've my mark up as

<div id="wrap">
   <div class="clickhere"></div>
   <div class="clickhere"></div>
   <div class="clickhere"></div>
   <div class="clickhere"></div>
   <div class="clickhere"></div>
</div>

Now, I want to add another <div class="clickhere"></div> just after the div clicked using jQuery. I thought of .append() or .prepend() but it only adds at the last or the first of the parent element (if applied at the parent element).

$('.clickhere').click(function(){
   // add <div class="clickhere"></div> just below $(this)
})

你有.insertAfter() :)

$('<div class="clickhere"></div>').insertAfter(this);

And in something approaching POJS:

> $('.clickhere').click(function(){

  var div = document.createElement('div');
  div.className = 'clickhere';
  this.parentNode.insertBefore(div, this.nextSibling);

> just below $(this) })

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