Sorting divs by number inside div tag and jQuery

前端 未结 4 1441
温柔的废话
温柔的废话 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:33

    $(function(){ 
        var sortedList = $('.contest_entry').toArray().sort(function(lhs, rhs){ 
            return parseInt($(rhs).children("span.votes").text(),10) - parseInt($(lhs).children("span.votes").text(),10); 
        });
        //the variable 'sortedList' holds the divs ordered. All you need to do is place them in the DOM.
    }); 
    

    Here is a working example: http://jsfiddle.net/ZCvUa/

提交回复
热议问题