Sorting divs by number inside div tag and jQuery

前端 未结 4 1392
温柔的废话
温柔的废话 2021-01-14 13:40

I\'m trying to sort a list of divs with jQuery. Essentially the list might look like this:

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-14 14:29

    Some thing along these lines should work:

    var list = [];
    
    function sortDivs(a,b)
    {
        return parseInt(a.text(), 10) - parseInt(b.text(), 10);
    }
    
    $('contest_entry').each(function () {
        list.push(this);
        $(this).detach();
    })
    
    list.sort(sortDivs)
    
    for (var x = 0; x < list.length; x++) {
        $('#parent_el').append(list[x]);//where parent_el is the place you want to reinsert the divs in the DOM
    }
    

提交回复
热议问题