开发者

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&开发者_StackOverflowgt;
</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)
})


You have .insertAfter() for that :)

$('<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) })
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜