I have a list of elements:
<div class="wrapper">
<div class="abc"></div>
<div class="abc"></div>
<div class="abc"></div>
</div>
And I have an array or numbers which represent new order:
var arr = [2,1,0];
I would like to reposition these abc divs to new positions inside parent wrapper.
Important thing is that abc divs have events and data attached to them and this needs to be preserved!
I have came up with this and it seems to be working as expected:
var wrapper = $('.wrapper'), items = wrapper.children('div.abc');
var orderedItems = $.map(arr, function(value) {
return $(items).clone(true,true).get(value);
});
wrapper.empty().html(orderedItems);
I wanted to make sure this is the right way.
I could do with javascript solution as well if possible.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…