【问题标题】:Sorting divs by number inside div tag and jQuery在 div 标签和 jQuery 中按数字对 div 进行排序
【发布时间】:2011-12-09 14:04:51
【问题描述】:

我正在尝试使用 jQuery 对 div 列表进行排序。基本上该列表可能如下所示:

<div class="contest_entry"><img src="image.png" /><span class="votes">14</span></div>
<div class="contest_entry"><img src="image.png" /><span class="votes">8</span></div>
<div class="contest_entry"><img src="image.png" /><span class="votes">2</span></div>
<div class="contest_entry"><img src="image.png" /><span class="votes">2</span></div>
<div class="contest_entry"><img src="image.png" /><span class="votes">2</span></div>
<div class="contest_entry"><img src="image.png" /><span class="votes">21</span></div>

我正在尝试使用一些 jQuery 自动将 div 按最高数字排序到最低数字。我该怎么办?感谢您的任何指导!我想我应该补充一点,排序应该发生在页面加载上。 :)

【问题讨论】:

标签: jquery sorting numbers


【解决方案1】:

我为此写了small plugin。随意偷。 基本上你选择元素,对它们进行排序,然后以新的顺序重新添加。

================================================ ================================

根据 Jason 的要求,包括此处的代码

$(".contest_entry").orderBy(function() {return +$(this).text();}).appendTo("#parent_div");

#parent_div.contest_entrys 的容器。

+ 只是一种将值转换为数字以强制进行数字比较而不是字符串比较的偷偷摸摸的方式(除非这是您想要的)。

orderBy() 是我写的一个排序插件。从那以后我稍微安静地扩展了它,但这里是简单的版本:

jQuery.fn.orderBy = function(keySelector)
{
    return this.sort(function(a,b)
    {
        a = keySelector.apply(a);
        b = keySelector.apply(b);
        if (a > b)
            return 1;
        if (a < b)
            return -1;
        return 0;
    });
};

【讨论】:

  • +1 做得很好@liho1eye。不过,您应该在答案中包含代码,以防链接停止工作并便于参考。
  • 谢谢。太棒了。像魅力一样工作!
【解决方案2】:

这些方面的一些事情应该有效:

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
}

【讨论】:

    【解决方案3】:
    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] + ']'));
    }
    

    【讨论】:

      【解决方案4】:
      $(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.
      }); 
      

      这是一个工作示例:http://jsfiddle.net/ZCvUa/

      【讨论】:

      • 仅供参考:jQuery 对象直接从数组中窃取其排序方法,因此无需来回转换。
      猜你喜欢
      • 2019-05-26
      • 1970-01-01
      • 2023-03-11
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      相关资源
      最近更新 更多