Invert order of HTML elements
I have the following configuration:
<div><div id='box1'> </div><div id='box2'> </div><div id='box3'> </div><div id='box4'> </div></div>
what i need to do is to invert the order of the divs
<div><div id='box4'> </div><div id='box3'> </div><div 开发者_开发知识库id='box2'> </div><div id='box1'> </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
精彩评论