开发者

Adding div before selected div

I have a bunch of divs one after the other.

<div class="betweenable">some content</div>
<div class="betweenable">other content</div>
<div class="betweenable">yet another</div>

When I click the last div, I want to insert the text new content in a div before it, so the final result will be

<div class="betweenable">some content</div>
<div class="betweenable">other content</div>
<div class="betweenable">new content</div>
<div class="betweenable">yet another开发者_如何转开发</div>

I tried append but it's adding the new markup inside the select div at the top. I want it inserted outside the selected div and right before it. What should I use instead of this line

var newmarkup = '<div class="betweenable">new content</div>';
$(this).prepend();


You can try this

$('.betweenable').click(function(){
   var newmarkup = '<div class="betweenable">new content</div>';
   $(this).before(newmarkup);
});

Here's an example : http://jsfiddle.net/jomanlk/C4XyH/


you can use .before here is DEMO

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜