Sorting divs by number inside div tag and jQuery

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

    var jVotes = $('div.contest_entry span.votes'), vals = [];
    jVotes.each(function() {
        var numVotes = $(this).html(); // may also be able to use .text()
        vals.push(numVotes);
        $(this).data('num-votes', numVotes);
    });
    vals.sort(function(a, b) {
        return (a < b) ? -1 : (a > b) ? 1 : 0;
    });
    var jParent = $('selector for parent container');
    for (var i = 0; i < jVotes.length; i++) {
        jParent.append(jParent.find('[data-num-votes=' + vals[i] + ']'));
    }
    

提交回复
热议问题