jQuery的實現(xiàn)原理的模擬代碼 -1 核心部分
更新時間:2010年08月01日 10:15:37 作者:
最近又看了一下 jQuery 1.4.2, 為了便于理解,將 jQuery 的核心使用比較簡單的代碼模擬一下。方便學習。
核心部分實現(xiàn)了兩種選擇器,使用 id 和標記名,還可以提供 css 的設置,以及 text 的設置。
// # 表示在 jQuery 1.4.2 中對應的行數(shù)
// 定義變量 undefined 方便使用
var undefined = undefined;
// jQuery 是一個函數(shù),其實調(diào)用 jQuery.fn.init 創(chuàng)建對象
var $ = jQuery = window.$ = window.jQuery // #19
= function (selector, context) {
return new jQuery.fn.init(selector, context);
};
// 用來檢查是否是一個 id
idExpr = /^#([\w-]+)$/;
// 設置 jQuery 的原型對象, 用于所有 jQuery 對象共享
jQuery.fn = jQuery.prototype = { // #74
length: 0, // #190
jquery: "1.4.2", // # 187
// 這是一個示例,僅僅提供兩種選擇方式:id 和標記名
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 {
// 直接使用標記名
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 對象的個數(shù)
size: function () { // #193
return this.length;
},
// 用來設置 css 樣式
css: function (name, value) { // #4564
this.each(
function (name, value) {
this.style[name] = value;
},
arguments // 實際的參數(shù)以數(shù)組的形式傳遞
);
return this;
},
// 用來設置文本內(nèi)容
text: function (val) { // #3995
if (val) {
this.each(function () {
this.innerHTML = val;
},
arguments // 實際的參數(shù)以數(shù)組的形式傳遞
)
}
return this;
},
// 用來對所有的 DOM 對象進行操作
// 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 對象中包含的元素
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 對象實際上是一個仿數(shù)組的對象,代表通過選擇器得到的所有 DOM 對象的集合,它像數(shù)組一樣有 length 屬性,表示代表的 DOM 對象的個數(shù),還可以通過下標進行遍歷。
95 行的 jQuery.each 是 jQuery 中用來遍歷這個仿數(shù)組,對其中的每個元素進行遍歷處理的基本方法,callback 表示處理這個 DOM 對象的函數(shù)。通常情況下,我們并不使用這個方法,而是使用 jQuery 對象的 each 方法進行遍歷。jQuery 對象的 css 和 text 方法在內(nèi)部實際上使用 jQuery 對象的 each 方法對所選擇的元素進行處理。
這些函數(shù)及對象的關(guān)系見:jQuery 原型關(guān)系圖

下面的腳本使用這個腳本庫。
// 原型操作
$("h1").text("Hello, world.").css("color", "green");
復制代碼 代碼如下:
// # 表示在 jQuery 1.4.2 中對應的行數(shù)
// 定義變量 undefined 方便使用
var undefined = undefined;
// jQuery 是一個函數(shù),其實調(diào)用 jQuery.fn.init 創(chuàng)建對象
var $ = jQuery = window.$ = window.jQuery // #19
= function (selector, context) {
return new jQuery.fn.init(selector, context);
};
// 用來檢查是否是一個 id
idExpr = /^#([\w-]+)$/;
// 設置 jQuery 的原型對象, 用于所有 jQuery 對象共享
jQuery.fn = jQuery.prototype = { // #74
length: 0, // #190
jquery: "1.4.2", // # 187
// 這是一個示例,僅僅提供兩種選擇方式:id 和標記名
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 {
// 直接使用標記名
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 對象的個數(shù)
size: function () { // #193
return this.length;
},
// 用來設置 css 樣式
css: function (name, value) { // #4564
this.each(
function (name, value) {
this.style[name] = value;
},
arguments // 實際的參數(shù)以數(shù)組的形式傳遞
);
return this;
},
// 用來設置文本內(nèi)容
text: function (val) { // #3995
if (val) {
this.each(function () {
this.innerHTML = val;
},
arguments // 實際的參數(shù)以數(shù)組的形式傳遞
)
}
return this;
},
// 用來對所有的 DOM 對象進行操作
// 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 對象中包含的元素
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 對象實際上是一個仿數(shù)組的對象,代表通過選擇器得到的所有 DOM 對象的集合,它像數(shù)組一樣有 length 屬性,表示代表的 DOM 對象的個數(shù),還可以通過下標進行遍歷。
95 行的 jQuery.each 是 jQuery 中用來遍歷這個仿數(shù)組,對其中的每個元素進行遍歷處理的基本方法,callback 表示處理這個 DOM 對象的函數(shù)。通常情況下,我們并不使用這個方法,而是使用 jQuery 對象的 each 方法進行遍歷。jQuery 對象的 css 和 text 方法在內(nèi)部實際上使用 jQuery 對象的 each 方法對所選擇的元素進行處理。
這些函數(shù)及對象的關(guān)系見:jQuery 原型關(guān)系圖

下面的腳本使用這個腳本庫。
復制代碼 代碼如下:
// 原型操作
$("h1").text("Hello, world.").css("color", "green");
相關(guān)文章
jquery實現(xiàn)仿新浪微博帶動畫效果彈出層代碼(可關(guān)閉、可拖動)
這篇文章主要介紹了jquery實現(xiàn)仿新浪微博帶動畫效果彈出層代碼,具有可關(guān)閉及可拖動的功能,涉及jQuery針對鼠標事件的響應及頁面元素屬性的變換功能,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10