这个 jquery 脚本的内部工作原理
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它本质上需要三个输入:
this
是您调用它的 jQuery 对象,例如,如果它是$("#my-id").sort()
那么 < code>this 将是$("#my-id")
comparator
是与 JavaScript 的原生Array.prototype.sort
getSortable< /code> 是一个可选的转换函数,在实际排序之前应用于已排序的 DOM 元素。它的行为对我来说仍然有点令人困惑;看来只有返回另一个 DOM 元素才有效?
它通过该行获取 Array.prototype.sort 方法,
然后将其与比较器函数一起使用来获取 jQuery 对象的排序版本,该版本是“类似数组” " 因为它具有属性
0
、1
、2
、... 和length
属性:然后循环通过结果,在 DOM 元素本身(默认)或给定 DOM 元素时返回的任何
getSortable
上调用placements[i]
:所以真正的魔力发生在创建过程中
placements
数组的。经过一番思考,我们发现placements[i]
是一个函数,它将其输入插入到this
的第i
元素所在的位置是,在排序发生之前。因此,使用第i
sorted 元素作为输入来调用placement[i]
会导致放置第i
>已排序元素位于第i
“槽”中,即第i
未排序元素所在的位置。在placements
数组创建中使用“标志”节点的整个过程是这样的,即使在您开始用新的排序元素替换它们之后,它也可以跟踪原始“槽”的位置。另外值得注意的是:它(在我看来是不必要的)将自身包装在一个立即执行的匿名函数中,该函数本身返回一个采用
comparator
和getSortable
参数的函数。因此,分配给 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()
thenthis
would be$("#my-id")
comparator
is a comparison function of the same type as accepted by JavaScript's nativeArray.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 lineIt then uses it with the
comparator
function to get a sorted version of the jQuery object, which is "array-like" since it has properties0
,1
,2
, ... and alength
property:It then loops through the result, calling
placements[i]
on either the DOM element itself (default) or whatevergetSortable
returns when given a DOM element:So the real magic happens in the creation of the
placements
array. After some thought, we see thatplacements[i]
is a function that inserts its input into the same place that thei
th element ofthis
was, before sorting happened. So callingplacement[i]
using thei
th sorted element as input results in placing thei
th sorted element in thei
th "slot", i.e. the place where thei
th unsorted element was. The whole business with "flag" nodes inside theplacements
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
andgetSortable
parameters. Thus the actual result assigned tojQuery.fn.sort
is the two-parameter function.