这个jQuery排序函数如何工作? [英] How does this jQuery sort function work?

查看:91
本文介绍了这个jQuery排序函数如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是
的后续问题如何在jQuery中按值对孩子进行排序

这是使用金额值按范围对一堆链接进行排序的代码:

This is the code for sorting a bunch of links by the span with the amount value:

$('#sortamount').on('click', function () {
    $('.entrylink').sort(function(a, b) {
        return $(a).find('.amount').text() - $(b).find('.amount').text();
    })
    .appendTo('#entrytable');
});

我不完全理解这一行:

return $(a).find('.amount').text() - $(b).find('.amount').text();

以及.appendTo如何在将新排序的条目链接附加到表之前清除表。如何进行排序? .appendTo会自动用新排序的内容替换入口表的内容吗?

and how .appendTo clears the table before appending the newly sorted entry links to the table. How does the sorting happen? Does .appendTo automatically replace the entrytable's contents with the newly sorted contents?

推荐答案

由于您使用算术运算,因此两者的值都是在 - 操作完成之前,LHS和RHS将转换为数字。

Since you are using an arithmetic operations the values of both the LHS and RHS are converted to a Number before the - operation is done.

这里我们实际上正在排序DOM元素,因此在排序后,您将以新的顺序获得DOM元素。当您在DOM中已经存在的一组DOM元素上使用append时,它将从旧位置移除并将被放置在新位置。

Here we are actually sorting the DOM elements, so after sorting you have the DOM elements in a new order. when you use append on a set of DOM elements which are already present in the DOM it will be removed from the old location and will be placed on the new location.

这篇关于这个jQuery排序函数如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆