自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

jQuery核心部分原理的模擬代碼

開發(fā) 開發(fā)工具
為了便于讀者理解jQuery 1.4的核心部分,文章將jQuery的核心使用比較簡單的代碼模擬一下。核心部分實(shí)現(xiàn)了兩種選擇器,使用id和標(biāo)記名,還可以提供CSS的設(shè)置,以及tex的設(shè)置。

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

51CTO推薦閱讀:jQuery四大天王:核心函數(shù)詳解

  1. //#表示在 jQuery 1.4.2 中對應(yīng)的行數(shù)  
  2.  
  3. // 定義變量 undefined 方便使用  
  4. var undefinedundefined = undefined;  
  5.  
  6. // jQuery 是一個(gè)函數(shù),其實(shí)調(diào)用 jQuery.fn.init 創(chuàng)建對象  
  7.  var $ = jQuery = window.$ = window.jQuery// #19 
  8.    = function (selector, context) {  
  9.  return new jQuery.fn.init(selector, context);  
  10.    };  
  11.  
  12. // 用來檢查是否是一個(gè) id  
  13.  idExpr = /^#([\w-]+)$/;  
  14.    
  15. // 設(shè)置 jQuery 的原型對象, 用于所有 jQuery 對象共享  
  16.  jQueryjQuery.fn = jQuery.prototype = {    // #74  
  17.    
  18. length: 0,  // #190  
  19. jquery: "1.4.2", // # 187  
  20.    
  21. // 這是一個(gè)示例,僅僅提供兩種選擇方式:id 和標(biāo)記名  
  22. init: function (selector, context) { // #75  
  23.    
  24.    // Handle HTML strings  
  25.     if (typeof selector === "string") {  
  26.    // Are we dealing with HTML string or an ID?  
  27.    match = idExpr.exec(selector);  
  28.  
  29.    // Verify a match, and that no context was specified for #id  
  30.    if (match && match[1]) {  
  31.   var elem = document.getElementById(match[1]);  
  32.   if (elem) {  
  33.  this.length = 1;  
  34. this[0] = elem;  
  35.   }  
  36.    }  
  37.    else {  
  38.  // 直接使用標(biāo)記名  
  39.   var nodes = document.getElementsByTagName(selector);  
  40.   for (var l = nodes.length, j = 0; j < l; j++) {  
  41. this[j] = nodes[j];  
  42.   }  
  43.   this.length = nodes.length;  
  44.    }  
  45.    
  46.    this.context = document;  
  47.    this.selector = selector;  
  48.    
  49.    return this;  
  50.     }  
  51. },  
  52.    
  53. // 代表的 DOM 對象的個(gè)數(shù)  
  54. size: function () {   // #193  
  55.     return this.length;  
  56. },  
  57.    
  58. // 用來設(shè)置 css 樣式  
  59. css: function (name, value) {   // #4564  
  60.     this.each(  
  61. function (name, value) {  
  62. this.style[name] = value;  
  63.  },  
  64.  arguments  // 實(shí)際的參數(shù)以數(shù)組的形式傳遞  
  65.  );  
  66.     return this;  
  67. },  
  68.    
  69. // 用來設(shè)置文本內(nèi)容  
  70. text: function (val) {// #3995  
  71.     if (val) {  
  72.   this.each(function () {  
  73.   this.innerHTML = val;  
  74.    },  
  75.  arguments  // 實(shí)際的參數(shù)以數(shù)組的形式傳遞  
  76.  )  
  77.     }  
  78.     return this;  
  79. },  
  80.    
  81. // 用來對所有的 DOM 對象進(jìn)行操作  
  82. // callback 自定義的回調(diào)函數(shù)  
  83. // args 自定義的參數(shù)  
  84. each: function (callback, args) {    // #244  
  85.     return jQuery.each(this, callback, args);  
  86. }  
  87.  
  88.  } 

  1.  // init 函數(shù)的原型也就是 jQuery 的原型  
  2.  jQueryjQuery.fn.init.prototype = jQuery.prototype;  // #303  
  3.  
  4.  // 用來遍歷 jQuery 對象中包含的元素  
  5.  jQuery.each = function (object, callback, args) {  // #550  
  6.    
  7. var i = 0length = object.length;  
  8.    
  9. // 沒有提供參數(shù)  
  10. if (args === undefined) {  
  11.    
  12.     for (var value = object[0];  
  13.  i < length && callback.call(value, i, value) !== false;  
  14.  value = object[++i])  
  15.     { }  
  16. }  
  17. else {  
  18.     for (; i < length; ) {  
  19.    if (callback.apply(object[i++], args) === false) {  
  20.   break;  
  21.    }  
  22.     }  
  23. }  
  24.  } 

在jQuery中, jQuery對象實(shí)際上是一個(gè)仿數(shù)組的對象,代表通過選擇器得到的所有DOM對象的集合,它像數(shù)組一樣有l(wèi)ength屬性,表示代表的DOM對象的個(gè)數(shù),還可以通過下標(biāo)進(jìn)行遍歷。

95行的jQuery.each是jQuery中用來遍歷這個(gè)仿數(shù)組,對其中的每個(gè)元素進(jìn)行遍歷處理的基本方法,callback表示處理這個(gè)DOM對象的函數(shù)。通常情況下,我們并不使用這個(gè)方法,而是使用 jQuery對象的each方法進(jìn)行遍歷。jQuery對象的css和text方法在內(nèi)部實(shí)際上使用jQuery對象的each方法對所選擇的元素進(jìn)行處理。

責(zé)任編輯:王曉東 來源: 博客園
相關(guān)推薦

2010-02-04 10:10:34

Dalvik虛擬機(jī)

2023-09-05 23:38:36

Kubernetes集群

2020-11-02 09:35:04

ReactHook

2020-12-03 08:14:45

Axios核心Promise

2011-08-31 10:14:11

2025-03-18 10:21:14

2023-12-16 10:40:58

2025-01-27 11:49:55

2020-11-09 07:29:12

ReentrantLo源碼公平鎖

2021-07-12 09:45:36

NameServer 核心Conusmer

2009-06-15 15:57:21

Spring工作原理

2012-05-22 09:52:03

jQuery

2021-04-28 10:13:58

zookeeperZNode核心原理

2021-04-21 07:52:39

核心SignalR應(yīng)用

2021-08-02 07:57:03

注冊Nacos源碼

2020-05-21 13:25:43

Spring組件架構(gòu)

2016-12-09 09:07:39

2025-01-02 11:06:22

2021-09-27 08:56:44

NettyChannelHand架構(gòu)

2023-05-08 14:56:00

Kafka高可靠高性能
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號