开发者

Invert order of HTML elements

开发者 https://www.devze.com 2022-12-27 12:10 出处:网络
I have the following configuration: <div><div id=\'box1\'>&nbsp;</div><div id=\'box2\'>&nbsp;</div><div id=\'box3\'>&nbsp;</div><div id=\'box4

I have the following configuration:

<div><div id='box1'>&nbsp;</div><div id='box2'>&nbsp;</div><div id='box3'>&nbsp;</div><div id='box4'>&nbsp;</div></div>

what i need to do is to invert the order of the divs

<div><div id='box4'>&nbsp;</div><div id='box3'>&nbsp;</div><div 开发者_开发知识库id='box2'>&nbsp;</div><div id='box1'>&nbsp;</div></div>

is there a fast way to do this with jQuery without cloning, removing and replacing the items?


Try this:

$top = $('div#thatTopDiv');
$top.children('div').slice(1).each(function() {
    $(this).insertBefore($top.children().eq(0));
});

Edit: Tested and this works.


I guess you're looking for jQuery Reverse Order plugin.


You could use the jQuery TinySort plugin to order the items by class name (descending)...

http://plugins.jquery.com/project/TinySort

0

精彩评论

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

关注公众号