ret.prevObject = this;
// Return the newly-formed element set
return ret;
}
jQuery.contains
这个函数是对 DOM 判断是否是父子关系,源码如下:
jQuery.contains = function (context, elem) {
// 考虑到兼容性,设置 context 的值
if ((context.ownerDocument || context) !== document) {
setDocument(context);
}
return contains(context, elem);
}// contains 是内部函数,判断 DOM_a 是否是 DOM_b 的
var contains = function (a, b) {
var adown = a.nodeType === 9 ? a.documentElement : a,
bup = b && b.parentNode;
return a === bup || !!(bup && bup.nodeType === 1 && (
adown.contains ? adown.contains(bup) : a.compareDocumentPosition && a.compareDocumentPosition(bup) & 16));
}
jQuery.uniqueSort
jQuery 的去重函数,但这个去重职能处理 DOM 元素数组,不能处理字符串或数字数组,来看看有什么特别的:
jQuery.uniqueSort = function (results) {
var elem, duplicates = [],
j = 0,
i = 0; // hasDuplicate 是一个判断是否有相同元素的 flag,全局
hasDuplicate = !support.detectDuplicates;
sortInput = !support.sortStable && results.slice(0);
results.sort(sortOrder);
if (hasDuplicate) {
while ((elem = results[i++])) {
if (elem === results[i]) {
j = duplicates.push(i);
}
}
while (j--) {
// splice 用于将重复的元素删除
results.splice(duplicates[j], 1);
}
}
// Clear input after sorting to release objects
// See https://github.com/jquery/sizzle/pull/225
sortInput = null;
return results;
}
sortOrder 函数如下,需要将两个函数放在一起理解才能更明白哦:
var sortOrder = function (a, b) { // 表示有相同的元素,设置 flag 为 true
if (a === b) {
hasDuplicate = true;
return 0;
}
// Sort on method existence if only one input has compareDocumentPosition
var compare = !a.compareDocumentPosition - !b.compareDocumentPosition;
if (compare) {
return compare;
}
// Calculate position if both inputs belong to the same document
compare = (a.ownerDocument || a) === (b.ownerDocument || b) ? a.compareDocumentPosition(b) :
// Otherwise we know they are disconnected
1;
// Disconnected nodes
if (compare & 1 || (!support.sortDetached && b.compareDocumentPosition(a) === compare)) {
// Choose the first element that is related to our preferred document
if (a === document || a.ownerDocument === preferredDoc && contains(preferredDoc, a)) {
return -1;
}
if (b === document || b.ownerDocument === preferredDoc && contains(preferredDoc, b)) {










