新聞中心
之前報道過《jQuery 1.4十大新特性解讀及代碼示例》,為了便于理解,將jQuery的核心使用比較簡單的代碼模擬一下。核心部分實現(xiàn)了兩種選擇器,使用id和標(biāo)記名,還可以提供CSS的設(shè)置,以及tex的設(shè)置。

在九江等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、成都網(wǎng)站制作 網(wǎng)站設(shè)計制作定制網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),網(wǎng)絡(luò)營銷推廣,成都外貿(mào)網(wǎng)站建設(shè),九江網(wǎng)站建設(shè)費用合理。
推薦閱讀:jQuery四大天王:核心函數(shù)詳解
- //#表示在 jQuery 1.4.2 中對應(yīng)的行數(shù)
- // 定義變量 undefined 方便使用
- var undefinedundefined = 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-]+)$/;
- // 設(shè)置 jQuery 的原型對象, 用于所有 jQuery 對象共享
- jQueryjQuery.fn = jQuery.prototype = { // #74
- length: 0, // #190
- jquery: "1.4.2", // # 187
- // 這是一個示例,僅僅提供兩種選擇方式: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 對象的個數(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ù)組的形式傳遞
- );
- return this;
- },
- // 用來設(shè)置文本內(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 的原型
- jQueryjQuery.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ù)組一樣有l(wèi)ength屬性,表示代表的DOM對象的個數(shù),還可以通過下標(biāo)進行遍歷。
95行的jQuery.each是jQuery中用來遍歷這個仿數(shù)組,對其中的每個元素進行遍歷處理的基本方法,callback表示處理這個DOM對象的函數(shù)。通常情況下,我們并不使用這個方法,而是使用 jQuery對象的each方法進行遍歷。jQuery對象的css和text方法在內(nèi)部實際上使用jQuery對象的each方法對所選擇的元素進行處理。
網(wǎng)頁名稱:jQuery核心部分原理的模擬代碼
文章起源:http://www.dlmjj.cn/article/cdsjege.html


咨詢
建站咨詢
