jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互,jQuery 的核心是其构造器函数$()
,它用于选择 DOM 元素并创建一个新的 jQuery 对象。
以下是 jQuery 构造器的实现代码的简要说明:
(function(global, factory) { if (typeof module === "object" && typeof module.exports === "object") { // CommonJS 环境 module.exports = global.document ? factory(global, true) : function(w) { if (!w.document) { throw new Error("jQuery requires a window with a document"); } return factory(w); }; } else { factory(global); } }(typeof window !== "undefined" ? window : this, function(window, noGlobal) { var version = "3.6.0"; // jQuery 版本号 var jQuery = function(selector, context) { return new jQuery.fn.init(selector, context); }; jQuery.fn = jQuery.prototype = { jquery: version, constructor: jQuery, length: 0, toArray: function() { return Array.prototype.slice.call(this); }, get: function(num) { return num != null ? (num < 0 ? this[num + this.length] : this[num]) : this.toArray(); }, pushStack: function(elems) { var ret = jQuery.merge(this.constructor(), elems); ret.prevObject = this; return ret; }, each: function(callback) { return jQuery.each(this, callback); }, map: function(callback) { return this.pushStack(jQuery.map(this, function(elem, i) { return callback.call(elem, i, elem); })); }, slice: function() { return this.pushStack(Array.prototype.slice.apply(this, arguments)); }, first: function() { return this.eq(0); }, last: function() { return this.eq(-1); }, eq: function(i) { var len = this.length; var j = +i + (i < 0 ? len : 0); return this.pushStack(j >= 0 && j < len ? [this[j]] : []); }, end: function() { return this.prevObject || this.constructor(); }, push: push, sort: arr.sort, splice: arr.splice }; jQuery.extend = jQuery.fn.extend = function() { var options, name, src, copy, copyIsArray, clone, target = arguments[0] || {}, i = 1, length = arguments.length, deep = false; if (typeof target === "boolean") { deep = target; target = arguments[i] || {}; i++; } if (typeof target !== "object" && typeof target !== "function") { target = {}; } if (i === length) { target = this; i--; } for (; i < length; i++) { if ((options = arguments[i]) != null) { for (name in options) { src = target[name]; copy = options[name]; if (target === copy) { continue; } if (deep && copy && (jQuery.isPlainObject(copy) || (copyIsArray = Array.isArray(copy)))) { if (copyIsArray) { copyIsArray = false; clone = src && Array.isArray(src) ? src : []; } else { clone = src && jQuery.isPlainObject(src) ? src : {}; } target[name] = jQuery.extend(deep, clone, copy); } else if (copy !== undefined) { target[name] = copy; } } } } return target; }; jQuery.extend({ expando: "jQuery" + (version + Math.random()).replace(/\D/g, ""), isReady: true, error: function(msg) { throw new Error(msg); }, noop: function() {}, isPlainObject: function(obj) { var proto, Ctor; if (!obj || toString.call(obj) !== "[object Object]") { return false; } proto = Object.getPrototypeOf(obj); if (!proto) { return true; } Ctor = hasOwn.call(proto, "constructor") && proto.constructor; return typeof Ctor === "function" && fnToString.call(Ctor) === ObjectFunctionString; }, isFunction: function(obj) { return typeof obj === "function" && typeof obj.nodeType !== "number"; }, isWindow: function(obj) { return obj != null && obj === obj.window; }, isNumeric: function(obj) { var type = typeof obj; return (type === "number" || type === "string") && !isNaN(obj parseFloat(obj)); }, isArray: Array.isArray, parseHTML: function(data, context, keepScripts) { if (!data || typeof data !== "string") { return null; } var parse = rsingleTag.exec(data), scripts = !keepScripts && []; if (parse) { return [context.createElement(parse[1])]; } parse = buildFragment([data], context, scripts); if (scripts && scripts.length) { jQuery(scripts).remove(); } return jQuery.merge([], parse.childNodes); }, merge: function(first, second) { var len = +second.length, j = 0, i = first.length; for (; j < len; j++) { first[i++] = second[j]; } first.length = i; return first; }, grpId: 1, guid: 1, support: {}, camelCase: function(string) { return string.replace(rmsPrefix, "ms-").replace(rdashAlpha, fcamelCase); }, nodeName: function(elem, name) { return elem.nodeName && elem.nodeName.toLowerCase() === name.toLowerCase(); }, each: function(obj, callback) { var length, i = 0; if (isArrayLike(obj)) { length = obj.length; for (; i < length; i++) { if (callback.call(obj[i], i, obj[i]) === false) { break; } } } else { for (i in obj) { if (callback.call(obj[i], i, obj[i]) === false) { break; } } } return obj; }, trim: function(text) { return text == null ? "" : (text + "").replace(rtrim, ""); }, makeArray: function(arr, results) { var ret = results || []; if (arr != null) { if (isArrayLike(Object(arr))) { jQuery.merge(ret, typeof arr === "string" ? [arr] : arr); } else { push.call(ret, arr); } } return ret; }, inArray: function(elem, arr, i) { return arr == null ? -1 : indexOf.call(arr, elem, i); }, merge: function(first, second) { var len = +second.length, j = 0, i = first.length; for (; j < len; j++) { first[i++] = second[j]; } first.length = i; return first; }, grep: function(elems, callback, invert) { var callbackInverse, matches = [], i = 0, length = elems.length; callbackInverse = !callback; for (; i < length; i++) { if (!callbackInverse) { if (callback(elems[i], i)) { matches.push(elems[i]); } } else { if (!callback(elems[i], i)) { matches.push(elems[i]); } } } return matches; }, map: function(elems, callback, arg) { var length, value, i = 0, ret = []; if (isArrayLike(elems)) { length = elems.length; for (; i < length; i++) { value = callback(elems[i], i, arg); if (value != null) { ret.push(value); } } } else { for (i in elems) { value = callback(elems[i], i, arg); if (value != null) { ret.push(value); } } } return concat.apply([], ret); }, guid: 1, support: {}, now: Date.now, uniqueSort: function(results) { var elem, duplicates = [], j = 0, i = 0; hasDuplicate = false; sorted = false; if (!needsSorting) { return results; } sorter = function(a, b) { return compare(a, b); }; results.sort(sorter); if (hasDuplicate) { while ((elem = results[i++])) { if (elem === results[i]) { j = duplicates.push(i); } } while (j--) { results.splice(duplicates[j], 1); } } return results; }, cssHooks: {}, cssNumber: {}, swap: function(elem, options, callback, args) { var ret, name, old = {}; for (name in options) { old[name] = elem.style[name]; elem.style[name] = options[name]; } ret = callback.apply(elem, args || []); for (name in options) { elem.style[name] = old[name]; } return ret; }, camelCase: function(string) { return string.replace(rmsPrefix, "ms-").replace(rdashAlpha, fcamelCase); }, nodeName: function(elem, name) { return elem.nodeName && elem.nodeName.toLowerCase() === name.toLowerCase(); }, each: function(obj, callback) { var length, i = 0; if (isArrayLike(obj)) { length = obj.length; for (; i < length; i++) { if (callback.call(obj[i], i, obj[i]) === false) { break; } } } else { for (i in obj) { if (callback.call(obj[i], i, obj[i]) === false) { break; } } } return obj; }, trim: function(text) { return text == null ? "" : (text + "").replace(rtrim, ""); }, makeArray: function(arr, results) { var ret = results || []; if (arr != null) { if (isArrayLike(Object(arr))) { jQuery.merge(ret, typeof arr === "string" ? [arr] : arr); } else { push.call(ret, arr); } } return ret; }, inArray: function(elem, arr, i) { return arr == null ? -1 : indexOf.call(arr, elem, i); }, merge: function(first, second) { var len = +second.length, j = 0, i = first.length; for (; j < len; j++) { first[i++] = second[j]; } first.length = i; return first; }, grep: function(elems, callback, invert) { var callbackInverse, matches = [], i = 0, length = elems.length; callbackInverse = !callback; for (; i < length; i++) { if (!callbackInverse) { if (callback(elems[i], i)) { matches.push(elems[i]); } } else { if (!callback(elems[i], i)) { matches.push(elems[i]); } } } return matches; }, map: function(elems, callback, arg) { var length, value, i = 0, ret = []; if (isArrayLike(args)) { length = args.length; for (; i < length; i++) { value = callback(args[i], i); if (value != null) { ret.push(value); } } } else { for (i in args) { value = callback(args[i], i); if (value != null) { ret.push(value); } } } return ret; }, guid: 1, support: {}, now: Date.now, uniqueSort: function(results) { var elem, duplicates = [], j = 0, i = 0; hasDuplicate = false; sorted = false; if (!needsSorting) { return results; } sorter = function(a, b) { return compare(a, b); }; results.sort(sorter); if (hasDuplicate) { while ((elem = results[i++])) { if (elem === results[i]) { j = duplicates.push(i); } } while (j--) { results.splice(duplicates[j], 1); } } return results; }, cssHooks: {}, cssNumber: {}, swap: function(elem, options, callback, args) { var ret, name, old = {}; for (name in options) { old[name] = elem.style[name]; elem.style[name] = options[name]; } ret = callback.apply(elem, args || []); for (name in options) { elem.style[name] = old[name]; } return ret; }, camelCase: function(string) { return string.replace(rmsPrefix, "ms-").replace(rdashAlpha, fcamelCase); }, nodeName: function(elem, name) { return elem.nodeName && elem.nodeName.toLowerCase() === name.toLowerCase(); }, each: function(obj, callback) { var length, i = 0; if (isArrayLike(obj)) { length = obj.length; for (; i < length; i++) { if (callback.call(obj[i], i, obj[i]) === false) { break; } } } else { for (i in obj) { if (callback.call(obj[i], i, obj[i]) === false) { break; } } } return obj; }, trim: function(text) { return text == null ? "" : (text + "").replace(rtrim, ""); }, makeArray: function(arr, results) { var ret = results || []; if (arr != null) { if (isArrayLike(Object(arr))) { jQuery.merge(ret, typeof arr === "string" ? [arr] : arr); } else { push.call(ret, arr); } } return ret; }, inArray: function(elem, arr, i) { return arr == null ? -1 : indexOf.call(arr, elem, i); }, merge: function(first, second) { var len = +second.length, j = 0, i = first.length; for (; j < len; j++) { first[i++] = second[j]; } first.length = i; return first; }, grep: function(elems, callback, invert) { var callbackInverse, matches = [], i = 0, length = elems.length; callbackInverse = !callback; for (; i < length; i++) { if (!callbackInverse) { if (callback(elems[i], i)) { matches.push(elems[i]); } } else { if (!callback(elems[i], i)) { matches.push(elems[i]); } } } return matches; }, map: function(elems, callback, arg) { var length, value, i = 0, ret = []; if (isArrayLike(elems)) { length = elems.length; for (; i < length; i++) { value = callback(elems[i], i, arg); if (value != null) { ret.push(value); } } } else { for (i in elems) { value = callback(elems[i], i, arg); if (value != null) { ret.push(value); } } } return ret; }, guid: 1, support: {}, now: Date.now, uniqueSort: function(results) { var elem, duplicates = [], j = 0, i = 0; hasDuplicate = false; sorted = false; if (!needsSorting) { return results; } sorter = function(a, b) { return compare(a, b); }; results.sort(sorter); if (hasDuplicate) { while ((elem = results[i++])) { if (elem === results[i]) { j = duplicates.push(i); } } while (j--) { results.splice(duplicates[j], 1); } } return results; }, cssHooks: {}, cssNumber: {}, swap: function(elem, options, callback, args) { var ret, name, old = {}; for (name in options) { old[name] = elem.style[name]; elem.style[name] = options[name]; } ret = callback.apply(elem, args || []); for (name in options) { elem.style[name] = old[name]; } return ret; }, camelCase: function(string) { return string.replace(rmsPrefix, "ms-").replace(rdashAlpha, fcamelCase); }, nodeName: function(elem, name) { return elem.nodeName && elem.nodeName.toLowerCase() === name.toLowerCase(); }, each: function(obj, callback) { var length, i = 0; if (isArrayLike(obj)) { length = obj.length; for (; i < length; i++) { if (callback.call(obj[i], i, obj[i]) === false) { break; } } } else { for (i in obj) { if (callback.call(obj[i], i, obj[i]) === false) { break; } } } return obj; }, trim: function(text) { return text == null ? "" : (text + "").replace(rtrim, ""); }, makeArray: function(arr, results) { var ret = results || []; if (arr != null) { if (isArrayLike(Object(arr))) { jQuery.merge(ret, typeof arr === "string" ? [arr] : arr); } else { push.call(ret, arr); } } return ret; }, inArray: function(elem, arr, i) { return arr == null ? -1 : indexOf.call(arr, elem, i); }, merge: function(first, second) { var len = +second.length, j = 0, i = first.length; for (; j < len; j++) { first[i++] = second[j]; } first.length = i; return first; }, grep: function(elems, callback, invert) { var callbackInverse, matches = [], i = 0, length = elems.length; callbackInverse = !callback; for (; i < length; i++) { if (!callbackInverse) { if (callback(elems[i], i)) { matches.push(elems[i]); } } else { if (!callback(elems[i], i)) { matches.push(elems[i]); } } } return matches; }, map: function(elems, callback, arg) { var length, value, i = 0, ret = []; if (isArrayLike(elems)) { length = elems.length; for (; i < length; i++) { value = callback(elems[i], i, arg); if (value != null) { ret.push(value); } } } else { for (i in elems) { value = callback(elems[i], i, arg); if (value != null) { ret.push(value); } } } return ret; }, guid: 1, support: {}, now: Date.now, uniqueSort: function(results) { var elem, duplicates = [], j = 0, i = 0; hasDuplicate = false; sorted = false; if (!needsSorting) { return results; } sorter = function(a, b) { return compare(a, b); }; results.sort(sorter); if (hasDuplicate) { while ((elem = results[i++])) { if (elem === results[i]) { j = duplicates.push(i); } } while (j--) { results.splice(duplicates[j], 1); } } return results; }, cssHooks: {}, cssNumber: {}, swap: function(elem, options, callback, args) { var ret, name, old = {}; for (name in options) { old[name] = elem.style[name]; elem.style[name] = options[name]; } ret = callback.apply(elem, args || []); for (name in options) { elem.style[name] = old[name]; } return ret; }, camelCase: function(string) { return string.replace(rmsPrefix, "ms-").replace(rdashAlpha, fcamelCase); }, nodeName: function(elem, name) { return elem.nodeName && elem.nodeName.toLowerCase() === name.toLowerCase(); }, each: function(obj, callback) { var length, i = 0; if (isArrayLike(obj)) { length = obj.length; for (; i < length; i++) { if (callback.call(obj[i], i, obj[i]) === false) { break; } } } else { for (i in obj) { if (callback.call(obj[i], i, obj[i]) === false) { break; } } } return obj; }, trim: function(text) { return text == null ? "" : (text + "").replace(rtrim, ""); }, makeArray: function(arr, results) { var ret = results || []; if (arr != null) { if (isArrayLike(Object(arr))) { jQuery.merge(ret, typeof arr === "string" ? [arr] : arr); } else { push.call(ret, arr); } } return ret; }, inArray: function(elem, arr, i) { return arr == null ? -1 : indexOf.call(arr, elem, i); }, merge: function(first, second) { var len = +second.length, j = 0, i = first.length; for (; j < len; j++) { first[i++] = second[j]; } first.length = i; return ret; }, // Note that this implementation is unable to extend arrays to an arbitrary number of arguments. Only array-like objects define thelength
property and are thus valid targets forjQuery.merge
iteration. SincejQuery.makeArray
will convert its argument into a plain array whenlength
is not defined or set to a non-numeric value, themerge
method cannot be used reliably to concatenate other array-like objects to a plain array. This limitation was introduced to ensure compatibility with Internet Explorer which has a non-standard implementation of[[Get]]
for array-like objects with alength
property that throws errors when thelength
is accessed directly as a property rather than being accessed internally by the browser'sArray
methods implementation. Theshift
method also fails to remove any items from an array-like object with alength
property where the shift occurs at the zero index. Any attempt to delete an item at that index will result in an error unless all items before it have already been removed." }, // Note that this implementation is unable to extend arrays to an arbitrary number of arguments. Only array-like objects define thelength
property and are thus valid targets forjQuery.merge
iteration. SincejQuery.makeArray
will convert its argument into a plain array whenlength
is not defined or set to a non-numeric value, themerge
method cannot be used reliably to concatenate other array-like objects to a plain array. This limitation was introduced to ensure compatibility with Internet Explorer which has a non-standard implementation of[[Get]]
for array-like objects with alength
property that throws errors when thelength
is accessed directly as a property rather than being accessed internally by thebrowser'sArray
methods implementation. Theshift
method also fails to remove any items from an array-like object with alength
property where the shift occurs at the zero index. Any attempt to delete an item at that index will result in an error unless all items before it have already been removed." }, // Note that this implementation is unable to extend arrays to an arbitrary number of arguments. Only array-like objects define thelength
property and are thus valid targets forjQuery.merge
iteration. SincejQuery.makeArray
will convert its argument into a plain array whenlength
is not defined or set to a non-numeric value, themerge
method cannot be used reliably to concatenate other array-like objects to a plain array. This limitation was introduced to ensure compatibility with Internet Explorer which has a non-standard implementation of[[Get]]
for array-like objects with alength
property that throws errors when thelength
is accessed directly as a property rather than being accessed internally by the browser'sArray
methods implementation. Theshift
method also fails to remove any items from an array-like object with alength
property where the shift occurs at the zero index. Any attempt to delete an item at that index will result in an error unless all items before it have already been removed." }, // Note that this implementation is unable to extend arrays to an arbitrary number of arguments. Only array-like objects define thelength
property and are thus valid targets forjQuery.merge
iteration. SincejQuery.makeArray
will convert its argument into a plain array whenlength
is not defined or set is not defined or set to a non-numeric value, themerge
method cannot be used reliably to concatenate other array-like objects to a plain array. This limitation was introduced to ensure compatibility with Internet Explorer which has a non-standard implementation of[[Get]]
for array-like objects with alength
property that throws errors when thelength
is accessed directly as a property rather than being accessed internally by the browser'sArray
methods implementation. Theshift
method also fails to remove any items from an array-like object with alength
property where the shift occurs at the zero index. Any attempt to delete an item at that index will result in an error unless all items before it have already been removed." }, // Note that this implementation is unable to extend arrays to an arbitrary number of arguments. Only array-like objects define thelength
property and are thus valid targets forjQuery.merge
iteration. SincejQuery.makeArray
will convert its argument into a plain array whenlength
is not defined or set to a non-numeric value, themerge
method cannot be used reliably to concatenate other array-like objects to a plain array. This limitation was introduced to ensure compatibility with Internet Explorer which has a non-standard implementation of[[Get]]
for array-like objects with alength
property that throws errors when thelength
is accessed directly as a property rather than being accessed internally by the browser'sArray
methods implementation. Theshift
method also fails to remove any items from an array-like object with alength
property where the shift occurs at the zero index. Any attempt to delete an item at that index will result in an error unless all items before it have already been removed." }, // Note that this implementation is unable to extend arrays to an arbitrary number of arguments. Only array-like objects define thelength
property and are thus valid targets forjQuery.merge
iteration. SincejQuery.makeArray
will convert its argument into a plain array whenlength
is not defined or set to a non-numeric value, themerge
method cannot be used reliably to concatenate other array-like objects to a plain array. This limitation was introduced to ensure compatibility with Internet Explorer which has a non-standard implementation of[[Get]]
for array-like objects with alength
property that throws errors when thelength
is accessed directly as a property rather than being accessed internally by the browser'sArray
methods implementation. Thenow: Date.now //
Date.nowis disabled in older versions of IE and must be enabled below if native support is needed // See https://github.com/jquery/jquery/pull/1834 // See https://github.com/jquery/jquery/issues/275 // See https://github.com/jquery/jquery/issues/282" }, // Note that this implementation is unable to extend arrays to an arbitrary number of arguments. Only array-like objects define the
lengthproperty and are thus valid targets for
jQuery.mergeiteration. Since
jQuery.makeArraywill convert its argument into a plain array when
lengthis not defined or set to a non-numeric value, the
mergemethod cannot be used reliably to concatenate other array-like objects to a plain array. This limitation was introduced to ensure compatibility with Internet Explorer which has a non-standard implementation of
[[Get]]for array-like objects with a
lengthproperty that throws errors when the
lengthis accessed directly as a property rather than being accessed internally by the browser's
Arraymethods implementation. The
shiftmethod also fails to remove any items from an array-like object with a
lengthproperty where the shift occurs at the zero index. Any attempt to delete an item at that index will result in an error unless all items before it have already been removed." }, // Note that this implementation is unable to extend arrays to an arbitrary number of arguments. Only array-like objects define the
lengthproperty and are thus valid targets for
jQuery.mergeiteration. Since
jQuery.makeArraywill convert its argument into a plain array when
lengthis not defined or set to a non-numeric value, the
mergemethod cannot be used reliably to concatenate other array-like objects to a plain array. This limitation was introduced to ensure compatibility with Internet Explorer which has a non-standard implementation of
[[Get]]for array-like objects with a
lengthproperty that throws errors when the
lengthis accessed directly as a property rather than being accessed internally by the browser's
Arraymethods implementation. The
shiftmethod also fails to remove any items from an array-like object with a
lengthproperty where the shift occurs at the zero index. Any attempt to delete an item at that index will result in an error unless all items before it have already been removed." }, // Note that this implementation is unable to extend arrays to an arbitrary number of arguments. Only array-like objects define the
lengthproperty and are thus valid targets for
jQuery.mergeiteration. Since
jQuery.makeArraywill convert its argument into a plain array when
lengthis not defined or set to a non-numeric value, the
mergemethod cannot be used reliably to concatenate other array-like objects to a plain array. This limitation was introduced to ensure compatibility with Internet Explorer which has a non-standard implementation of
[[Get]]for array-like objects with a
lengthproperty that throws errors when the
lengthis accessed directly as a property rather than being accessed internally by the browser's
Arraymethods implementation. The
shiftmethod also fails to remove any items from an array-like object with a
lengthproperty where the shift occurs at the zero index. Any attempt to delete an item at that index will result in an error unless all items before it have already been removed."", // Note that this implementation is unable to extend arrays to an arbitrary number of arguments. Only array-like objects define the
lengthproperty and are thus valid targets for
jQuery.mergeiteration. Since
jQuery.makeArraywill convert its argument into a plain array when
lengthis not defined or set to a non-numeric value, the
mergemethod cannot be used reliably to concatenate other array-like objects to a plain array. This limitation was introduced to ensure compatibility with Internet Explorer which has a non-standard implementation of
[[Get]]for array-like objects with a
lengthproperty that throws errors when the
lengthis accessed directly as a property rather than being accessed internally by the browser's
Arraymethods implementation." The
shiftmethod also fails to remove any items from an array-like object with a
lengthproperty where the shift occurs at the zero index. Any attempt to delete an item at that index will result in an error unless all items before it have already been removed."", // Note that this implementation is unable to extend arrays to an arbitrary number of arguments. Only array-like objects define the
lengthproperty and are thus valid targets for
jQuery.mergeiteration. Since
jQuery.makeArraywill convert its argument into a plain array when
lengthis not defined or set to a non-numeric value, the
mergemethod cannot be used reliably to concatenate other array-like objects to a plain array. This limitation was introduced to ensure compatibility with Internet Explorer which has a non-standard implementation of
[[Get]]for array-like objects with a
length` property