这个 jquery 脚本的内部工作原理

发布于 2024-11-07 21:46:44 字数 1586 浏览 0 评论 0原文

我有一个 Jquery 脚本,它依赖于此脚本 Sort.js 。任何人都可以吗?这是如何工作的

jQuery.fn.sort = (function(){

    var sort = [].sort;

    return function(comparator, getSortable) {

        getSortable = getSortable || function(){return this;};

        var placements = this.map(function(){

              var sortElement = getSortable.call(this),               
                parentNode = sortElement.parentNode,                      
                // Since the element itself will change position, we have
                // to have some way of storing it's original position in
                // the DOM. The easiest way is to have a 'flag' node:
                nextSibling = parentNode.insertBefore(
                    document.createTextNode(''),
                    sortElement.nextSibling
                );

            return function() {

                if (parentNode === this) {
                    throw new Error(
                        "You can't sort elements if any one is a descendant of another."
                    );
                }

                // Insert before flag:
                parentNode.insertBefore(this, nextSibling);
                // Remove flag:
                parentNode.removeChild(nextSibling);

            };

        });

        return sort.call(this, comparator).each(function(i){
            placements[i].call(getSortable.call(this));
        });

    };

})();

我从这个链接 http:// /qd9.co.uk/projects/jQuery-Plugins/sort/demo.html

I have a Jquery Script which is depenedent on this Script Sort.js . Can any body How does this works

jQuery.fn.sort = (function(){

    var sort = [].sort;

    return function(comparator, getSortable) {

        getSortable = getSortable || function(){return this;};

        var placements = this.map(function(){

              var sortElement = getSortable.call(this),               
                parentNode = sortElement.parentNode,                      
                // Since the element itself will change position, we have
                // to have some way of storing it's original position in
                // the DOM. The easiest way is to have a 'flag' node:
                nextSibling = parentNode.insertBefore(
                    document.createTextNode(''),
                    sortElement.nextSibling
                );

            return function() {

                if (parentNode === this) {
                    throw new Error(
                        "You can't sort elements if any one is a descendant of another."
                    );
                }

                // Insert before flag:
                parentNode.insertBefore(this, nextSibling);
                // Remove flag:
                parentNode.removeChild(nextSibling);

            };

        });

        return sort.call(this, comparator).each(function(i){
            placements[i].call(getSortable.call(this));
        });

    };

})();

I got this Script from this link http://qd9.co.uk/projects/jQuery-Plugins/sort/demo.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆萌少年 2024-11-14 21:46:44

它本质上需要三个输入:

  • this 是您调用它的 jQuery 对象,例如,如果它是 $("#my-id").sort() 那么 < code>this 将是 $("#my-id")
  • comparator 是与 JavaScript 的原生 Array.prototype.sort
  • getSortable< /code> 是一个可选的转换函数,在实际排序之前应用于已排序的 DOM 元素。它的行为对我来说仍然有点令人困惑;看来只有返回另一个 DOM 元素才有效?

它通过该行获取 Array.prototype.sort 方法,

var sort = [].sort;

然后将其与比较器函数一起使用来获取 jQuery 对象的排序版本,该版本是“类似数组” " 因为它具有属性 012、... 和 length 属性:

sort.call(this, comparator)

然后循环通过结果,在 DOM 元素本身(默认)或给定 DOM 元素时返回的任何 getSortable 上调用 placements[i]

sort.call(this, comparator).each(function(i){
        placements[i].call(getSortable.call(this));
    });

所以真正的魔力发生在创建过程中placements 数组的。经过一番思考,我们发现 placements[i] 是一个函数,它将其输入插入到 this 的第 i 元素所在的位置是,排序发生之前。因此,使用第 isorted 元素作为输入来调用 placement[i] 会导致放置第 i >已排序元素位于第i“槽”中,即第i未排序元素所在的位置。在 placements 数组创建中使用“标志”节点的整个过程是这样的,即使在您开始用新的排序元素替换它们之后,它也可以跟踪原始“槽”的位置。

另外值得注意的是:它(在我看来是不必要的)将自身包装在一个立即执行的匿名函数中,该函数本身返回一个采用 comparatorgetSortable 参数的函数。因此,分配给 jQuery.fn.sort 的实际结果是二参数函数。

It takes essentially three inputs:

  • this is the jQuery object you are calling it on, e.g. if it was $("#my-id").sort() then this would be $("#my-id")
  • comparator is a comparison function of the same type as accepted by JavaScript's native Array.prototype.sort
  • getSortable is an optional transformation function that is applied to the sorted DOM elements before they are actually sorted. Its behavior is a bit confusing to me still; it appears it would only work if it returned another DOM element?

It gets the Array.prototype.sort method via the line

var sort = [].sort;

It then uses it with the comparator function to get a sorted version of the jQuery object, which is "array-like" since it has properties 0, 1, 2, ... and a length property:

sort.call(this, comparator)

It then loops through the result, calling placements[i] on either the DOM element itself (default) or whatever getSortable returns when given a DOM element:

sort.call(this, comparator).each(function(i){
        placements[i].call(getSortable.call(this));
    });

So the real magic happens in the creation of the placements array. After some thought, we see that placements[i] is a function that inserts its input into the same place that the ith element of this was, before sorting happened. So calling placement[i] using the ith sorted element as input results in placing the ith sorted element in the ith "slot", i.e. the place where the ith unsorted element was. The whole business with "flag" nodes inside the placements array creation is so that it can track the position of the original "slots" even after you start replacing them with new, sorted elements.

Also worth noting: it (rather unnecessarily IMO) wraps itself in an immediately-executing anonymous function which itself returns a function taking the comparator and getSortable parameters. Thus the actual result assigned to jQuery.fn.sort is the two-parameter function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文