Sort DOM elements to order array with jQuery

后端 未结 3 1082
轮回少年
轮回少年 2020-12-18 12:02

A moment ago I asked this question: javascript sort array to align with order array based on what turned out to be a false premise. (I\'ve been up for too long, give me a br

3条回答
  •  误落风尘
    2020-12-18 13:04

    I know this is late, but maybe it will help someone. I was trying to do a similar thing and #Felix Kling's answer got me 90% there. But in my case I have a div id which is equal to the order values, like this:

    order    = [3,1,4,0,2];
    
    
    Jack
    Jill
    Nancy
    Tom
    Cartman

    then I can simply push the element using the position value

    var elements = [];
    $.each(order, function(i, position) {
        elements.push($('#'+position));
    });
    
    $('#moduleWrap').append(elements);
    

提交回复
热议问题