开发者

How to rearrange elements using jQuery?

开发者 https://www.devze.com 2023-02-09 06:48 出处:网络
How to rearrange elements using jQuery ? Orginal Code : <p id=\"paragraph1\">1</p> <p id=\"paragraph2\">2</p>

How to rearrange elements using jQuery ?

Orginal Code :

<p id="paragraph1">1</p>
<p id="paragraph2">2</p>
<p id="paragraph3">3</p>
<p id="paragraph4">4</p>
<p id="paragraph5">5</p>

After Rearrange (put p3 in p2's place)

<p id="paragraph1">1</p>
<p id="paragraph3">3</p>
<p id="paragraph2">2</p>
<p id="paragraph4"&开发者_JAVA百科gt;4</p>
<p id="paragraph5">5</p>


You can use .insertBefore():

$("#paragraph3").insertBefore("#paragraph2");

Slightly more elaborate example (clicking on a paragraph moves it up):

$("p").click(function() {
   $(this).insertBefore($(this).prev()); 
});

You can test both examples here.

0

精彩评论

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

关注公众号