jQuery的實(shí)現(xiàn)原理的模擬代碼 -1 核心部分
更新時(shí)間:2010年08月01日 10:15:37 作者:
最近又看了一下 jQuery 1.4.2, 為了便于理解,將 jQuery 的核心使用比較簡(jiǎn)單的代碼模擬一下。方便學(xué)習(xí)。
核心部分實(shí)現(xiàn)了兩種選擇器,使用 id 和標(biāo)記名,還可以提供 css 的設(shè)置,以及 text 的設(shè)置。
// # 表示在 jQuery 1.4.2 中對(duì)應(yīng)的行數(shù)
// 定義變量 undefined 方便使用
var undefined = undefined;
// jQuery 是一個(gè)函數(shù),其實(shí)調(diào)用 jQuery.fn.init 創(chuàng)建對(duì)象
var $ = jQuery = window.$ = window.jQuery // #19
= function (selector, context) {
return new jQuery.fn.init(selector, context);
};
// 用來檢查是否是一個(gè) id
idExpr = /^#([\w-]+)$/;
// 設(shè)置 jQuery 的原型對(duì)象, 用于所有 jQuery 對(duì)象共享
jQuery.fn = jQuery.prototype = { // #74
length: 0, // #190
jquery: "1.4.2", // # 187
// 這是一個(gè)示例,僅僅提供兩種選擇方式:id 和標(biāo)記名
init: function (selector, context) { // #75
// Handle HTML strings
if (typeof selector === "string") {
// Are we dealing with HTML string or an ID?
match = idExpr.exec(selector);
// Verify a match, and that no context was specified for #id
if (match && match[1]) {
var elem = document.getElementById(match[1]);
if (elem) {
this.length = 1;
this[0] = elem;
}
}
else {
// 直接使用標(biāo)記名
var nodes = document.getElementsByTagName(selector);
for (var l = nodes.length, j = 0; j < l; j++) {
this[j] = nodes[j];
}
this.length = nodes.length;
}
this.context = document;
this.selector = selector;
return this;
}
},
// 代表的 DOM 對(duì)象的個(gè)數(shù)
size: function () { // #193
return this.length;
},
// 用來設(shè)置 css 樣式
css: function (name, value) { // #4564
this.each(
function (name, value) {
this.style[name] = value;
},
arguments // 實(shí)際的參數(shù)以數(shù)組的形式傳遞
);
return this;
},
// 用來設(shè)置文本內(nèi)容
text: function (val) { // #3995
if (val) {
this.each(function () {
this.innerHTML = val;
},
arguments // 實(shí)際的參數(shù)以數(shù)組的形式傳遞
)
}
return this;
},
// 用來對(duì)所有的 DOM 對(duì)象進(jìn)行操作
// callback 自定義的回調(diào)函數(shù)
// args 自定義的參數(shù)
each: function (callback, args) { // #244
return jQuery.each(this, callback, args);
}
}
// init 函數(shù)的原型也就是 jQuery 的原型
jQuery.fn.init.prototype = jQuery.prototype; // #303
// 用來遍歷 jQuery 對(duì)象中包含的元素
jQuery.each = function (object, callback, args) { // #550
var i = 0, length = object.length;
// 沒有提供參數(shù)
if (args === undefined) {
for (var value = object[0];
i < length && callback.call(value, i, value) !== false;
value = object[++i])
{ }
}
else {
for (; i < length; ) {
if (callback.apply(object[i++], args) === false) {
break;
}
}
}
}
在 jQuery 中, jQuery 對(duì)象實(shí)際上是一個(gè)仿數(shù)組的對(duì)象,代表通過選擇器得到的所有 DOM 對(duì)象的集合,它像數(shù)組一樣有 length 屬性,表示代表的 DOM 對(duì)象的個(gè)數(shù),還可以通過下標(biāo)進(jìn)行遍歷。
95 行的 jQuery.each 是 jQuery 中用來遍歷這個(gè)仿數(shù)組,對(duì)其中的每個(gè)元素進(jìn)行遍歷處理的基本方法,callback 表示處理這個(gè) DOM 對(duì)象的函數(shù)。通常情況下,我們并不使用這個(gè)方法,而是使用 jQuery 對(duì)象的 each 方法進(jìn)行遍歷。jQuery 對(duì)象的 css 和 text 方法在內(nèi)部實(shí)際上使用 jQuery 對(duì)象的 each 方法對(duì)所選擇的元素進(jìn)行處理。
這些函數(shù)及對(duì)象的關(guān)系見:jQuery 原型關(guān)系圖

下面的腳本使用這個(gè)腳本庫。
// 原型操作
$("h1").text("Hello, world.").css("color", "green");
復(fù)制代碼 代碼如下:
// # 表示在 jQuery 1.4.2 中對(duì)應(yīng)的行數(shù)
// 定義變量 undefined 方便使用
var undefined = undefined;
// jQuery 是一個(gè)函數(shù),其實(shí)調(diào)用 jQuery.fn.init 創(chuàng)建對(duì)象
var $ = jQuery = window.$ = window.jQuery // #19
= function (selector, context) {
return new jQuery.fn.init(selector, context);
};
// 用來檢查是否是一個(gè) id
idExpr = /^#([\w-]+)$/;
// 設(shè)置 jQuery 的原型對(duì)象, 用于所有 jQuery 對(duì)象共享
jQuery.fn = jQuery.prototype = { // #74
length: 0, // #190
jquery: "1.4.2", // # 187
// 這是一個(gè)示例,僅僅提供兩種選擇方式:id 和標(biāo)記名
init: function (selector, context) { // #75
// Handle HTML strings
if (typeof selector === "string") {
// Are we dealing with HTML string or an ID?
match = idExpr.exec(selector);
// Verify a match, and that no context was specified for #id
if (match && match[1]) {
var elem = document.getElementById(match[1]);
if (elem) {
this.length = 1;
this[0] = elem;
}
}
else {
// 直接使用標(biāo)記名
var nodes = document.getElementsByTagName(selector);
for (var l = nodes.length, j = 0; j < l; j++) {
this[j] = nodes[j];
}
this.length = nodes.length;
}
this.context = document;
this.selector = selector;
return this;
}
},
// 代表的 DOM 對(duì)象的個(gè)數(shù)
size: function () { // #193
return this.length;
},
// 用來設(shè)置 css 樣式
css: function (name, value) { // #4564
this.each(
function (name, value) {
this.style[name] = value;
},
arguments // 實(shí)際的參數(shù)以數(shù)組的形式傳遞
);
return this;
},
// 用來設(shè)置文本內(nèi)容
text: function (val) { // #3995
if (val) {
this.each(function () {
this.innerHTML = val;
},
arguments // 實(shí)際的參數(shù)以數(shù)組的形式傳遞
)
}
return this;
},
// 用來對(duì)所有的 DOM 對(duì)象進(jìn)行操作
// callback 自定義的回調(diào)函數(shù)
// args 自定義的參數(shù)
each: function (callback, args) { // #244
return jQuery.each(this, callback, args);
}
}
// init 函數(shù)的原型也就是 jQuery 的原型
jQuery.fn.init.prototype = jQuery.prototype; // #303
// 用來遍歷 jQuery 對(duì)象中包含的元素
jQuery.each = function (object, callback, args) { // #550
var i = 0, length = object.length;
// 沒有提供參數(shù)
if (args === undefined) {
for (var value = object[0];
i < length && callback.call(value, i, value) !== false;
value = object[++i])
{ }
}
else {
for (; i < length; ) {
if (callback.apply(object[i++], args) === false) {
break;
}
}
}
}
在 jQuery 中, jQuery 對(duì)象實(shí)際上是一個(gè)仿數(shù)組的對(duì)象,代表通過選擇器得到的所有 DOM 對(duì)象的集合,它像數(shù)組一樣有 length 屬性,表示代表的 DOM 對(duì)象的個(gè)數(shù),還可以通過下標(biāo)進(jìn)行遍歷。
95 行的 jQuery.each 是 jQuery 中用來遍歷這個(gè)仿數(shù)組,對(duì)其中的每個(gè)元素進(jìn)行遍歷處理的基本方法,callback 表示處理這個(gè) DOM 對(duì)象的函數(shù)。通常情況下,我們并不使用這個(gè)方法,而是使用 jQuery 對(duì)象的 each 方法進(jìn)行遍歷。jQuery 對(duì)象的 css 和 text 方法在內(nèi)部實(shí)際上使用 jQuery 對(duì)象的 each 方法對(duì)所選擇的元素進(jìn)行處理。
這些函數(shù)及對(duì)象的關(guān)系見:jQuery 原型關(guān)系圖

下面的腳本使用這個(gè)腳本庫。
復(fù)制代碼 代碼如下:
// 原型操作
$("h1").text("Hello, world.").css("color", "green");
相關(guān)文章
jQuery實(shí)現(xiàn)鼠標(biāo)移入移出事件切換功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)鼠標(biāo)移入移出事件切換功能,結(jié)合實(shí)例形式分析了jQuery不同版本處理鼠標(biāo)事件響應(yīng)與觸發(fā)相關(guān)操作技巧,需要的朋友可以參考下2018-09-09jQuery實(shí)現(xiàn)的圖片輪播效果完整示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的圖片輪播效果,結(jié)合完整實(shí)例形式分析了jQuery結(jié)合時(shí)間函數(shù)動(dòng)態(tài)改變頁面元素樣式的相關(guān)技巧,需要的朋友可以參考下2016-09-09jquery實(shí)現(xiàn)仿新浪微博帶動(dòng)畫效果彈出層代碼(可關(guān)閉、可拖動(dòng))
這篇文章主要介紹了jquery實(shí)現(xiàn)仿新浪微博帶動(dòng)畫效果彈出層代碼,具有可關(guān)閉及可拖動(dòng)的功能,涉及jQuery針對(duì)鼠標(biāo)事件的響應(yīng)及頁面元素屬性的變換功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10基于jQuery的時(shí)間戳與日期間的轉(zhuǎn)化
這篇文章主要為大家詳細(xì)介紹了基于jQuery的時(shí)間戳與日期間的轉(zhuǎn)化,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06jQuery之浮動(dòng)窗口實(shí)現(xiàn)代碼(兩種方法)
今天公司要求實(shí)現(xiàn)浮動(dòng)窗口效果,自己看了不少資料終于實(shí)現(xiàn)此效果。用jQ實(shí)現(xiàn)浮動(dòng)窗口功能,彈出窗口時(shí)背景變暗.2010-09-09